[Next] [Previous] [Up] [Top] [Contents]

CHAPTER 6 Special Unix Features

6.2 File Redirection


Output redirection takes the output of a command and places it into a named file. Input redirection reads the file as input to the command. The following table summarizes the redirection options.

File Redirection
SymbolRedirection
>output redirect
>!same as above, but overrides noclobber option of csh
>>append output
>>!same as above, but overrides noclobber option on csh and creates the file if it doesn't already exist.
|pipe output to another command
<input redirection
<<Stringread from standard input until "String" is encountered as the only thing on the line. Also known as a "here document" (see Chapter 8).
<<\Stringsame 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 from file.

To pipe output to another command use the form:

command | command

This command makes the output of the first command the input of the second command.

6.2.1 - Csh
6.2.2 - Sh

Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]