1. Interpretation 1
>/ DEV/null 2> & 1: redirect all standard output and error output to/dev/null, that is, discard all generated information.
command> file 2> file and command> file 2> & 1 are different.
first, command> file 2> file means to send the standard output information and error output information generated by the command to file. Command> file 2> file: Both stdout and stderr are directly sent to file. The file will be opened twice, so that stdout and stderr will overwrite each other, in this case, both fd1 and fd2 are used to seize the file pipeline at the same time.
the command> file 2> & 1 directly sends stdout to file. stderr inherits the fd1 pipeline and then sends it to file. File is opened only once, and only one pipeline fd1 is used, which includes stdout and stderr contents.
Io efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing a shell script, in most cases, we use command> file 2> & 1.
II. Explanation 2
about shell:> /dev/null 2> & 1 Details
it is often seen in shell:> /dev/null 2> & 1
you can define the output result in the form of %>
decompose the combination: ">/dev/null 2> & 1" contains five parts.
1:> indicates the redirection location, for example: echo "123">/home/123.txt
2: /dev/null indicates an empty Device File
3: 2> stderr standard error
4: & indicates equivalent meaning, 2> & 1, indicating that 2's output redirection is equivalent to 1
5:1 indicates stdout standard output, the default value is 1, therefore, ">/dev/null" is equivalent to "1>/dev/null"
therefore,> /dev/null 2> & 1 can also be written as "1>/dev/null 2> & 1"
the statement execution process in the title of this article is as follows:
1>/dev/null: first, it indicates that the standard output is redirected to an empty device file, that is, no information is output to the terminal. In other words, no information is displayed.
2> & 1: Next, the standard error output is redirected to the standard output. Because the standard output has been redirected to the empty device file, the standard error output is also redirected to the empty device file.
Original article:
Http://blog.sina.com.cn/s/blog_646753190100hwnr.html