Standard error and other output redirection
In addition to the standard input and standard output, commands often produce other types of output, such as error or status messages known as diagnostic output. Like standard output, standard error output is written to the screen unless it is redirected.
To redirect standard error or other output, use a file descriptor. A file descriptor is a number associated with each of the I/O files that a command ordinarily uses. File descriptors can also be specified to redirect standard input and standard output. The following numbers are associated with standard input, output, and error:
Item | Description |
---|---|
0 | Standard input (keyboard) |
1 | Standard output (display) |
2 | Standard error (display) |
To redirect standard error output, type the file descriptor number 2
in
front of the output or append redirection symbols (>
or > >
)
and a file name after the symbol. For example, the following command
takes the standard error output from the cc command
where it is used to compile the testfile.c file
and appends it to the end of the ERRORS file:
cc testfile.c 2>> ERRORS
Other types of output can also be redirected using the file descriptors
from 0
through 9
. For example, if
the cmd command writes output to file descriptor 9
,
you can redirect that output to the savedata file
with the following command:
cmd 9> savedata
If a command writes to more than one output, you can independently redirect each one. Suppose that a command directs its standard output to file descriptor 1, directs its standard error output to file descriptor 2, and builds a data file on file descriptor 9. The following command line redirects each of these outputs to a different file:
command > standard 2> error 9> data