CSE80 -- Lecture 4, Apr 25 -- I/O Redirection


This part discussed I/O redirection in the Bourne shell. This notation does not work with the C-shell family of shells; the C-shell only allows you to make standard error go to the same place as standard output if both are redirected to a file, and provides no mechanism to dup or otherwise manipulated descriptors.

Output Redirection

Sending Output to a New File

The > notation is used to redirect output of a command to a file.
$ echo "hello world" > foo
will echo the string "hello world" into a file called foo. If the file already exists, it is first truncated to zero length; if it doesn't already exist, it is created.

In general, the syntax is

$ command N> filename
where N is the output descriptor that you want to refer to the file filename when running command.

Appending Output to a File

The >> notation is used to append the output of a command to a file. The output of the command is added to the end of the file, so the original contents are not disturbed.
$ echo "log entry, stardate 59.34:  in standard orbit" >> log
sends the output of echo to the file log, but rather than truncating the file (if it already exists), the new data is appended to the end.

In general, the syntax is

$ command N>> filename
where N is the output descriptor that you want to refer to the file filename when running command.

Sending Output to Same Place as Another Descriptor

Sometimes you want to make output through one descriptor act as if you had used another descriptor. For example:
$ echo "this program is going to barf" >&2
">&2" tells the shell to redirect the output of echo to go to the object to which I/O descriptor 2 (stderr) refers. By convection, stderr typically refers to the user's terminal, though it may be itself redirected to an error log.

The dup system call is used to make descriptor 1 (stdout) from 2 (discussed in Lecture 1, i.e. make 1 refer to the same thing as 2 does. (Read this as "dups 1 from 2".) This notation is typically used to generate an error message that will not be captured by a pipe.

In general, the syntax is:

$ command N>&M
where N is the I/O descriptor number that you want to make refer to the same thing as I/O descriptor M when running command.

I/O Descriptor Conventions
0 stdin standard input
1 stdout standard output
2 stderr standard error

Note: X can be omitted if it is the standard output (stdout). Therefore, ">&2" is equivalent to "1>&2".

Input Redirection

The obvious transform of this syntax works for input redirections via "<" too. The default descriptor is 0, i.e., stdin.

Closing Descriptors

Instead of making descriptors refer to a file or to the same object as another existing descriptor, you may also close a descriptor. The syntax is:
$ showdesc <&-
  2: crw--w----, owner cs80s (8207), group tty (22), tty, dev(20,5) [W R S]
  1: crw--w----, owner cs80s (8207), group tty (22), tty, dev(20,5) [W R S]
which runs the showdesc command with no standard input. The showdesc program prints out the list of active descriptors and system information about them. It is not a standard Unix utility -- it is something I wrote some years ago -- but it is useful for determining what descriptors are active and to what kinds of objects do they refer.

Note the c before the file access permissions. That says that the descriptor is referring to a character device -- in this case, a teletype device. This is the interface through which your xterm talks to shells and other text-mode programs. If the output includes ino followed by a number, this means the object referred to by the descriptor is a file and it refers to the inode number of a file, which is what determines where the data in the file is stored on the disk drive.

You can try the following experiments to gain a better understanding of what the redirections are doing:

$ showdesc -f showdesc.out > file 2>&1 ; cat showdesc.out
$ showdesc -f showdesc.out 2>&1 > file ; cat showdesc.out
$ ( showdesc -f showdesc.out 1>&2) 2> file ; cat showdesc.out
$ (showdesc -f showdesc.out 3>&2 2>&1 1>&3) > file ; cat showdesc.out
$ (showdesc -f showdesc.out 3>&2 2>&1 1>&3 3>&-) > file ; cat showdesc.out
$ showdesc -f showdesc.out | cat; cat showdesc.out
$ echo hi | showdesc
If you can explain to yourself what is happening in each case and why showdesc's output is what it is, then you have a very good understanding of I/O redirection.

back forward


[ CSE 80 | ACS home | CSE home | CSE calendar | bsy's home page ]
picture of bsy

bsy@cse.ucsd.edu, last updated Thu May 23 13:04:12 PDT 1996.

email bsy