6.1 Standard Input and Standard Output
What happens if you don't give a filename argument on a command line? Most programs will take their input from your keyboard instead (after you press Return to start the program running, that is). Your Terminal keyboard is the program's standard input.
As a program runs, the results are usually displayed on your Terminal screen. The Terminal screen is the program's standard output. So, by default, each of these programs takes its information from the standard input and sends the results to the standard output. These two default cases of input/ output (I/O) can be varied. This is called I/O redirection.
If a program writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol (>) operator. The pipe operator (|) sends the standard output of one program to the standard input of another program. Input/output redirection is one of the most powerful and flexible Unix features.
If a program doesn't normally read from files, but reads from its standard input, you can give a filename by using the less-than symbol (<) operator. tr (character translator) is such a program. Here's how to use the input redirection operator to convert commas to linefeeds in the to_do file:
$ cat to_do
Install Mac OS X,Learn Unix,???,Profit!
$ tr ',' '\n' < to_do
Install Mac OS X
Learn Unix
???
Profit!
$
Can you see what's happened here? The tr command has translated every comma in the input file (to_do, which replaced standard input because of the < notation) to a carriage return, displaying the output on standard output (the Terminal window).
Connect With Us