What does 2>&1 mean in linux shell
In a unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command:
2>&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 is a file descriptor and not a filename.
So the construct becomes: 2>&1
.
What's the meaning of '>/dev/null 2>&1' in shell ?
> is for redirect
/dev/null is a black hole where any data sent, will be discarded
2 is the file descriptor for Standard Error
> is for redirect
& is the symbol for file descriptor (without it, the following 1 would be considered a filename)
1 is the file descriptor for Standard Out
Therefore >/dev/null 2>&1 is redirect the output of your program to /dev/null. Include both the Standard Error and Standard Out.
stdin ==> fd 0
stdout ==> fd 1
stderr ==> fd 2
/dev/null causing:
stdin ==> fd 0
stdout ==> /dev/null
stderr ==> fd 2
2>&1 causing:
stdin ==> fd 0
stdout ==> /dev/null
stderr ==> stdout
tar cf /var/tmp/whateverfile.tar whateverfile > /dev/null 2>&1