You can also specify additional file descriptors, designating them by a number 3 through 9, and redirect I/O through them.
Symbol | Redirection |
---|---|
> | output redirect |
>! | same as above, but overrides noclobber option of csh |
>> | append output |
>>! | |
>& file | redirect stdout and stderr to file |
>>& | append stdout and stderr to file |
| | pipe output to another command |
|& command | pipe stdout and stderr to command |
< | input redirection |
<<String | read from standard input until "String" is encountered as the only thing on the line. Also known as a "here document" (see Chapter 8). |
<<\String | same as above, but don't allow shell substitutions |
An example of output redirection is:
cat file1 file2 > file3
The above command concatenates file1 then file2 and redirects (sends) the output to file3. If file3 doesn't already exist it is created. If it does exist it will either be truncated to zero length before the new contents are inserted, or the command will be rejected, if the noclobber option of the csh is set. (See the csh in Chapter 4). The original files, file1 and file2, remain intact as separate entities.
Output is appended to a file in the form:
cat file1 >> file2
This command appends the contents of file1 to the end of what already exists in file2. (Does not overwrite file2).
Input is redirected from a file in the form:
program < file
This command takes the input for program
To redirect stdout and stderr to two separate files you need to first redirect stdout in a sub-shell, as in:
% (command > out_file) >& err_file
To pipe output to another command
command | command
This command makes the output of the first command the input of the second command.
cat -n stuffcalls the cat program (to ``concatenate'' files). In this case, cat reads the file named ``stuff'' and displays it. The -n flag tells cat to number the lines in the display.
The hyphen that precedes a flag is a special character used to distinguish flags from filenames. In the example above, the hyphen prevents cat from trying to display a file named ``n''. Commands can contain other special characters as well. The shell interprets such characters, and sometimes replaces them with other values, before it passes the command with its flags and arguments to the program that actually executes the command.
Remember that uppercase and lowercase letters are not interchangeable. Thus if you tried to give the cat command by typing CAT, it would fail; there is no program named (uppercase) CAT. Or notice in the echo command shown above that SHELL (and only SHELL) must be all uppercase.
whothe login names of others currently logged in are written to standard output--displayed at your terminal. If you want to have this list placed into a file called ``namelist'', you could use the character > to redirect standard output to namelist, like this:
who > namelistIf you already have a file named namelist, the shell erases its contents, then calls who to write the new information to namelist. If you do not have a file named namelist, the shell creates a new one before it calls the who program.
You can then do what you want with the file namelist--print it, sort its contents, display it with the cat command, or whatever.
To append output to an existing file instead of overwriting it, use the symbol >>. Thus, if you first type
who > namelistand later
who >> namelistThe file namelist will contain two sets of results: the second set is appended to the first instead of destroying it.
Normally, you will not need to redirect standard input, as most commands that can read from standard input also accept an input filename as an argument. However, for commands that normally expect input from your terminal, the character < followed by a filename tells the command to read from that file instead. For example, one way to send mail is to type
mail userand then to type the text of the message from your terminal. If the text of the message is already in a file named letter, you could redirect standard input to send the mail this way:
mail user < letter
Note: The shell performs input and output redirection BEFORE it calls the command you want to execute. This means that files can be accidentally destroyed. For example, the commandsort < myfile > myfiledestroys ``myfile'' because the shell (1) opens myfile for reading and then (2) opens it again for writing. When you open a file for writing, UNIX destroys the old contents of the file. All this happens BEFORE the shell runs the sort program to actually read the file.
who > namelist sort namelistAfterward, you could give another command to get rid of the file namelist.
A pipeline enables you to do all this on a single command line, using the pipe symbol | (vertical bar). When commands are separated by the | symbol, output from the command on the symbol's left side becomes input to the one on the right of it. Thus you could type
who | sortso that the results of the who program are passed to the sort program as input and displayed, by default, on your terminal.
More than one pipe symbol can be used to make a series of commands, the standard output from each becoming the standard input to the next.
Note: If a command is not capable of reading from standard input, it cannot be placed to the right of a pipe symbol.