|internal file descriptor||An integer.|
|direction||One of |
|external file descriptor or path|
UNIX console programs have an input file and two output files (input and output streams, as well as devices, are treated as files by the OS.) These are typically the keyboard and screen, respectively, but any or all of them can be redirected to come from — or go to — a file or other program.
STDIN is standard input, and is how the program receives interactive input.
STDIN is usually assigned file descriptor 0.
STDOUT is standard output. Whatever is emitted on
STDOUT is considered the "result" of the program.
STDOUT is usually assigned file descriptor 1.
STDERR is where error messages are displayed. Typically, when running a program from the console,
STDERR is output on the screen and is indistinguishable from
STDERR is usually assigned file descriptor 2.
The order of redirection is important
command > file 2>&1
Redirects both (
STDERR) to the file.
command 2>&1 > file
STDOUT, because the file descriptor 2 is redirected to the file pointed to by file descriptor 1 (which is not the file
file yet when the statement is evaluated).
Each command in a pipeline has its own
STDOUT) because each is a new process. This can create surprising results if you expect a redirect to affect the entire pipeline. For example this command (wrapped for legibility):
$ python -c 'import sys;print >> sys.stderr, "Python error!"' \ | cut -f1 2>> error.log
will print "Python error!" to the console rather than the log file. Instead, attach the error to the command you want to capture:
$ python -c 'import sys;print >> sys.stderr, "Python error!"' 2>> error.log \ | cut -f1