Today in one of their technical group was asked such a question, and then the popular explanation, make a record, we see whether the explanation is clear. The shell may often see: >/dev/null 2>&1 The result of the command can be defined as the output in the form of%> Decompose this combination: ">/dev/null 2>&1" is part of five. Where 1:> represent redirects, such as echo "123" >/home/123.txt 2:/dev/null represents empty device files 3:2> indicates stderr standard error 4:& means equal to, 2>&1, indicating 2 output redirection equals 1 5:1 indicates stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null" Therefore, >/dev/null 2>&1 can also be written as "1>/dev/null 2> &1" Then the statement execution procedure for the title of this article is: 1>/dev/null: First of all, the standard output redirect to the empty device file, that is, not outputting any information to the terminal, plainly is not displaying any information. 2>&1: The standard error output is then redirected to the standard output, because the standard output is redirected to the empty device file, so the normal error output is redirected to the empty device file. Is that clear, you understand? By the way, tell the benefits of doing so. The most common ways are: Command > File 2>file with command > file 2>&1 Do they have any different places? First command > file 2>file means that the standard output information generated by the command and the wrong output information are sent to file. Command > File 2>file StdOut and stderr are sent directly to file, file will be opened two times, so that stdout and stderr will cover each other, which writes quite a lot of FD1 and FD2 two of the pipelines to preempt file. and command >file 2>&1 This order will stdout directly to file, StdErr inherited the FD1 pipeline, and then sent to file, at this time, file was only opened once, also only used a pipeline FD1, It includes the contents of stdout and stderr. In terms of IO efficiency, the previous command is less efficient than the one in the latter, so when writing a shell script, more often than not, we will write the command > file 2>&1. (Editor: South African Ant)
|