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.
$ echo "hello world" > foowill 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> filenamewhere N is the output descriptor that you want to refer to the file filename when running command.
$ echo "log entry, stardate 59.34: in standard orbit" >> logsends 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>> filenamewhere N is the output descriptor that you want to refer to the file filename when running command.
$ 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>&Mwhere 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".
$ 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 | showdescIf 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.

bsy@cse.ucsd.edu, last updated