next up previous contents
Next: Filename Substitution Up: The C Shell Previous: Command Execution

Redirection and Pipelines

The previous section demonstrated the power of passing the output of a command to somewhere other than to the screen - to another command. This is a very useful feature, and the C shell provides two ways other than command substitution to accomplish it. The first of these is redirections. Redirection allows the output of a command, or more generally, group of commands, to a file or device. There are four basic types of redirection operations. The command > filename redirection sends the output from the command to a file called filename. If this file does not exist it is created and the output is put there. If the file does exist, any contents that might be contained in it are erased and then the output is placed into the empty file. The command < filename redirection takes the contents of filename and uses them as input to command. The command >> filename works precisely the same as > with the exception that if the file it is to write to exists, it appends the output to the end of the file rather than eraseing the previous contents. The command << takes input from the keyboard as input. These four basic redirections make up the basis for table 4.1 which contains all of the redirections possible in the C shell:

  table216
Table 4.1:  Redirections possible in the C shell.

Notice that all of the redirections with the ! symbol refer to noclobber having been set. noclobber is a predefined shell variable (refer to the section on variables) that prevents existing files from being overwritten by redirection. The ! symbol overrides this variable if it is set. It is also important to realize that when the << redirection is used, the shell will make command, filename, and variable substitutions unless the input string is surrounded by quotes of some form. For each of these substitutions, a device, such as the null device, or a file descriptor could be put in place of file.

Another way to pass output of a command to another command is by using a pipe (|). The pipe is similar to the left single quoted command substitution described above, with the exception that it is formatted differently:

command1 | command2

which executes command1 and passes the result to command2. For example, the following are equivalent:

% wc `ls`

is the same as

% ls | wc

Pipelines, redirections, and command substitions can be mixed in any way that the user desires.


next up previous contents
Next: Filename Substitution Up: The C Shell Previous: Command Execution

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998