Up to main menu         Back to Aliases         Next: Manual Pages

Standard Input and Standard Output

For many programs and Unix commands, the text output goes directly to the screen unless you arrange otherwise. This is called ``standard output'' or stdout. Likewise, many command/programs expect input from the keyboard, considered ``standard input'' (stdin).

In many cases you don't want output to be printed straight to the screen, but to be filtered or redirected to a file. Likewise, you may want the input to a program to come from a file, rather than you having to type it in from the keyboard. For this you use pipes and arrows.

Redirecting output to a file
Use the ``>'' character after a command to direct stdout into a new file. for example,
		pascal>  date > today.txt
will put the results of the date command into file today.txt, rather than onto the monitor. If there was already a file by that name, its previous contents will be wiped out, and replaced with the new command.

If you wish to append stdout to the end of an existing file, without destroying that file's previous contents, then use ``>>>'' instead, e.g.

		pascal>  date >> today.txt

Getting input from a file
Use the ``<'' character to use the contents of a file as input to a program, replacing the input that would otherwise come from typing at the keyboard. For example, a program which calculates the roots of a quadratic equation might require you to type in the values of three coefficients a, b, c, before returning the roots of ax² + bx + c = 0.

	pascal>  a.out
                    
	    Enter the coefficient of x^2:    1
	    Enter the coefficient of x:      3
	    Enter the constant coefficient:  2

	Root #1 is  x=   -1.000000
	Root #2 is  x=   -2.000000
Rather than typing in the three numbers yourself, you could put the numbers into a file, say ``coeffs.txt'', and then run the program like this:
	pascal>  a.out < coeffs.txt
	Root #1 is  x=   -1.000000
	Root #2 is  x=   -2.000000

Redirecting output through a pipe
Use the ``|'' (pipe) character to connect the stdout of one program/command to the stdin of another. For instance, perhaps you have another executable program, a file named ran3, which spits out three random numbers. You could hook up the output of ran3 to the input of the earlier program a.out, which would use the three random numbers as coefficients of the quadratic equation:
	pascal>  ran3  |  a.out
	Root #1 is  x=  317.137829
	Root #2 is  x=   -0.702707

Filtering, using pipes
A common use of piping is to filter the textual output of a command through one or more other commands which might format, reorder, or otherwise modify the stream of text.

	pascal>  man -k edit | sort -u | wc | a.out
This 4-part command starts with the man -k command, which lists all the Unix commands related to the keyword ``edit''. This list is not shown on the monitor; rather, it is sorted and duplicate lines thrown out by the sort -u command. This result, in turn, is sent to wc (word count) which outputs the number of lines, words, and characters in the sorted list. Finally, these 3 numbers are used as input to a.out, which treats them as coefficients and produces the two roots of a quadratic equation as in the examples above. These two roots are the only output which appears on the monitor; all the intermediate steps are temporary in nature, and not displayed or saved in a file.

Up to main menu         Back to Aliases         Next: Manual Pages
Bruce.Fast@Colorado.EDU