The input stream is referred to as "standard input"; the output stream is referred to as "standard output"; and the error stream is referred to as "standard error". These terms are abbreviated to form the symbols used to refer to these files, namely stdin, stdout, and stderr.
On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are defined with these values in <unistd.h>.
There are also symbolic constants defined in unistd.h for the file descriptors belonging to the standard streams stdin
, stdout
, and stderr
; see Standard Streams.
STDIN_FILENO
This macro has value
0
, which is the file descriptor for standard input.STDOUT_FILENO
This macro has value
1
, which is the file descriptor for standard output.STDERR_FILENO
This macro has value
2
, which is the file descriptor for standard error output.Ref:
https://www.gnu.org/software/libc/manual/html_node/Descriptors-and-Streams.html
https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean?noredirect=1&lq=1
File descriptor 1 is the standard output (stdout
).
File descriptor 2 is the standard error (stderr
).
Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1
may look like a good way to redirect stderr
to stdout
. However, it will actually be interpreted as "redirect stderr
to a file named 1
". &
indicates that what follows and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1
.
Consider >&
as redirect merger operator.