This article was reprinted from: 11595985
By default, there are always three files open, standard input (keyboard input), standard output (output to screen), standard error (also output to screen), and their respective file descriptors are 0,1,2. So let's take a look at the differences between the following redirection methods:
>/dev/null 2>&1
In fact, it should be equivalent to this: 1>/dev/null 2>/dev/null, by default is 1, standard output, so generally omitted. The & symbol, followed by the required file descriptor. cannot be written as 2>1, so it becomes a standard error redirect to a file named 1, instead of redirecting standard errors to standard output. So here it is: The standard output is redirected to the/dev/null, and the standard error is redirected to the standard output, so the standard output and standard error are redirected to the/dev/null
2>&1 >/dev/null
At first glance, what's the difference between this and that one? Standard error redirected to standard output, and standard output redirected to/dev/null? Shouldn't we all redirect/dev/null at the end? That's how I understand it! An instruction either produces a standard error at the same time or produces a standard output. When the output standard is wrong, the standard error is redirected to the standard output, and the standard output is output to the screen. At this point the standard output was not redirected to/dev/null, so it was printed on the screen. When the standard output is produced, then it is not a standard error, 2>&1 is not valid, so the standard output redirects dev/null, does not print to the screen. So the end result will be: standard error printing to the screen, and standard output not printing to the screen.
-------------
2016-01-22 Update
Suddenly see the blog n years ago, only to find that there are a great error. Update to avoid astray, in fact, command line redirection is ready before executing the command. The explanation sequence is left to right, 2&>1, and 1 is the screen, so the standard error redirects to the screen, and 1>/dev/null, the standard output redirects to/dev/null, the 2>&1 >/dev/null above is not The same time either produces standard output or produces a standard error. It's a two different thing.
To explain it in the following variable way, it is obvious that these two ways are different, the former is like:
A=1
B=a
And the latter is like:
B=a
A=1
&>/dev/null
This is, whatever you are. File descriptor, all redirected to/dev/null
The difference between >/dev/null 2>&1 in the shell and 2>&1 >/dev/null and &>/dev/null