Normally, the output from command lines shows up on your screen. You type in a command and press , and the resulting output appears. This is actually a special case of the more general-purpose way that Unix handles both input and output.
All Unix commands come with two input/output devices, called stdin (for standard input ) and stdout (for standard output ). You can't see the stdin and stdout devices the same way you see a printer, but they are always there. You might like to think of stdin and stdout as valves or hose connectors stuck on the outside of every command. Think of the stdout connector as being fed to your screen, and your keyboard feeding data to the stdin connector.
You have seen redirection of stdout with the > and >> operators earlier in this chapter. In this section, you will learn more about redirecting stdout , and also how to redirect stdin that is, to have a command get input from someplace other than the keyboardand how to connect the stdout of one command to the stdin of another, creating what is called a pipeline . This ability to connect several commands together is one of the most important features of Unix's flexibility.
Besides stdin and stdout , there is one more virtual connection on each command, stderr (for standard error ), which is the output connection for warning and error messages. If a command issues an error message, it comes out of the stderr connector, which is normally connected to your screen (same as stdout ). If you redirect stdout to a file (with > ), stderr still goes to your screen. You can redirect stderr as well, though.
stdin , stdout , and stderr are often capitalized in Unix manuals and literature. Both upper- and lowercase usage is correct.
Figure 2.33 illustrates the concept of the stdin , stdout , and stderr connectors.
The most common form of redirection is to redirect the output of a command into a file using the > operator, which you saw in examples earlier in this chapter. You will redirect stdout to a file when you want to save the output for later use, such as editing or viewing.
To save output in a file:
redirects the output into the file named users. If the file does not already exist, it is created. If the file does exist, the contents are irrevocably overwritten .
Sometimes you'll want to simply throw away the output of a command without ever seeing it. To do this, redirect the output into the special file called /dev/null . For example,
noisy_command > /dev/null
Anything written to /dev/null is simply discarded. It's a good place to send insults and complaints.
Sometimes you will want to add the redirected output to a file instead of overwriting the contents.
To append output to a file:
stderr can be redirected as well. One reason to do this is to save errors into a log file. Another reason is to not have warning and error messages cluttering up your screen.
To redirect stderr to a file:
(If you are using the tcsh shell, you cannot redirect stdout and stderr separately, but you can make them both go into the same file using >& .) For example:
command >& filename
Sometimes you'll want a command to get its input from a file you have prepared. For example, you might have prepared the text of an e-mail message and want to feed it into the mail command, or you might have a list of filenames you want to feed into a program.
Most Unix commands allow you to redirect standard input and have it come from a file instead of the keyboard.
To take standard input from a file: