Previous Table of Contents Next

C Shell
The C shell uses the same basic notation for I/O redirection as the Bourne and Korn shell. It does, however, support a few different notations. They are as follows.
> name Writes standard output to the named file. Checks the noclobber option; if it is set, an existing file is not overwritten. If it is not set, an existing file is overwritten.
>! name Writes standard output to the named file. Overwrites an existing file without checking the noclobber option.
>& name Reroutes the diagnostic output (standard error) to the standard output, so both are written to the named file.
>&! A combination of the previous forms. Overrides noclobber and places standard error with standard output.
|& Passes diagnostic output to the pipe along with standard output.

The shell keyword commands can be redirected. This allows compound commands to be redirected to different output destinations.

Terminal Input and Output

The shell can access a device name that always refers to your terminal screen and keyboard, /dev/tty. You can use /dev/tty to explicitly send the output of a command to the terminal screen, even if the output of the shell script has been redirected to a file or pipe. You can also use /dev/tty to explicitly read from the terminal keyboard. The formats for using /dev/tty follow:

     echo "Press Return to Continue\c" > /dev/tty

     read NAME < /dev/tty

Garbage Can Output

The shell can access a device name that dispenses of all output. Referred to as the "garbage can," /dev/null can be used to redirect unwanted output. The format for using /dev/null follows:

     if grep dog /etc/passwd > /dev/null
     then  echo Someone has a login name of dog!

Redirecting Output in Loops

The standard I/O can be redirected in multiple ways within a shell or shell script. One of the best ways to show the flexibility of redirection is in a shell loop. The following example provides a brief view of how you can redirect I/O to and from different destinations within one loop.

     cat somefile | \
     while read LINE
       read ANS < /dev/tty
       echo $ANS:$LINE > /dev/tty
       echo $ANS:$LINE
      done > someoutput

This example pipes the data in somefile to the standard input of the while loop. The read LINE command reads one line of somefile for each iteration of the while loop. The read ANS command reads a line of input from your keyboard. The /dev/tty device is a logical channel that is connected to your keyboard (and your terminal). The output of the first echo is sent to your screen. The output of the entire loop (the second echo command) is sent to the file someoutput.

If you did not use the "< /dev/tty" on the read command, the next line of the input file would be read into the ANS variable. And if the "> /dev/tty" notation was not used on the echo command, the output would go to the someoutput file.


The primary application of Input/Output is for communicating with the computer system. By using the shells read, select, echo, and other commands, I/O can be presented in a formatted output and read in a formatted input. This allows for more user friendly menu building, data entry, or report generation.

Redirection is used to control where the input is read and where the output is sent. It can be used to store screen output to files. Input can be read from files, allowing you to enter all the necessary input in a file, then redirected to the desired command or file.


In this activity you use the redirection symbols to send output to a file and then have the shell redirect the standard input into a program. Begin at the shell prompt.

1.  Type ps -ef > psout and press Return. Notice no output is returned to your screen. The output of ps was redirected to the psout file.

BSD (Berkeley)
On BSD systems type ps -aux > psout and press Return.

2.  Type cat < psout and press Return. The file is displayed on your screen. The cat command is reading from the standard input. The shell opened the psout file and placed the data on the standard input, allowing cat to read the data. The result is the same as cat psout.
3.  Type notacommand and press Return. You should get an error displayed.
4.  Now type notacommand > stdout 2> stderr and press Return. Now type cat stdout and press Return. Nothing is displayed! Type cat stderr and press Return, notice the error is displayed. The shell sends its errors to the standard error not the standard output.

C Shell
Now type notacommand >& stderr and press Return. Now type cat stderr and press Return, notice the error is displayed. Also, note the csh redirects both the stdout and the stderr to the same file. This is one of the weaknesses of the csh output redirection function.

5.  Type rm psout stdout stderr and press Return to remove the "psout" file.
6.  Turn to Module 99 to continue the learning sequence.

Previous Table of Contents Next