next up previous contents index
Next: Pipes Up: UNIX Shells Previous: Processes

Standard Input, Output and Redirection

The shell and many UNIX commands take their input from standard input  ( stdin), write output to standard output  ( stdout), and write error output to standard error  ( stderr). By default, standard input is connected to the terminal keyboard and standard output and error to the terminal screen.

The way of indicating an end-of-file  on the default standard input, a terminal, is <ctrl-d>.

Redirection  of I/O is accomplished by specifying the destination on the command line using a redirection metacharacter followed by the desired destination. Using the C shell as an example, some of the forms are:


The first three redirect to files. These characters are easy to remember if you think of them as the head of an arrow. The fourth is called a pipe and is described in Section gif.

The form of a command with input and output redirection is:

 
               		   command [options] [arguments] < input-file > output file

Unless you are using the C shell and you have noclobber  set (see the man pages for csh), using > and >& to redirect output will overwrite any existing file of that name. You can use >> and >>& to append to existing files.

Examples:

 
  who > names       		 # Direct standard output to a file named names

(pwd; ls -l) > out # Direct output of both commands

pwd; ls -l > out # Direct output of ls command only

Input redirection can be useful if you have written a FORTRAN program which normally expects input from the terminal and you want to provide it from a file. In the following example myprog, which was written to read standard input and write standard output, is redirected to read the file myin and to write file myout. (All these examples use the C shell; syntax may vary slightly for other shells.)

 
               		  myprog   <myin >myout

You can suppress redirected output by sending it to the null device , /dev/null:
 
               		  who  >& /dev/null

To redirect standard error and output to different files, you can use grouping:
 
               		  (cat   myfile > myout) >& myerror



next up previous contents index
Next: Pipes Up: UNIX Shells Previous: Processes



Alan Silverman
Wed Apr 12 16:54:02 METDST 1995