unix redirect output to file 2>&1 code example
Example: 2 &1 meaning
$ cat foo.txt 1> output.txt # output stdout to output.txt
$ cat nop.txt 2> error.txt # output stderr to error.txt
stdout.
A file descriptor is nothing more that a positive integer that represents an open file.
If you have 100 open files, you will have 100 file descriptors for them.
The only caveat is that, in Unix systems, everything is a file.
There are file descriptors for the Standard Output (stdout, 1) and Standard Error (stderr, 2).
You use &1 to reference the value of the file descriptor 1 (stdout).
So when you use 2>&1 you are basically saying
“Redirect the stderr to the same place we are redirecting the stdout”.
(& - get address of 1, similar to C grammar.)
$cat foo.txt > output.txt 2>&1
$ cat nop.txt > output.txt 2>&1