Unix has several input and output streams, which correspond to numbers as follows:
0-standard input stream (stdin)
1-standard output stream (stdout)
2-standard error stream (stderr)
Command results can be defined in the form of>.
1./dev/null indicates that the device file is empty.
2. Redirection symbols:> redirect output to overwrite the file;
> Redirected output without overwriting the file;
1 indicates stdout standard output. The default value is 1. Therefore, ">/dev/null" is equivalent to "1>/dev/null". If no number is added, the default Redirection action is for stdout (1). For example, "ls-L> result" is equivalent to "ls-L 1> result ".
& Indicates the meaning of "equivalent", 2> & 1, indicating that 2's output redirection is equivalent to 1;
1>/dev/null 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 the standard error output redirection is equivalent 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, that is: is to redirect stderr to stdout and display it together on the screen.
CMD>/dev/null 2>/dev/null and CMD>/dev/null 2> & 1:
1. CMD>/dev/null 2>/dev/null: Send the standard output information and error output information generated by the command to/dev/null, stdout and stderr are both directly sent to/dev/null, And/dev/null will be opened twice, so that stdout and stderr will overwrite each other, in this way, both fd1 and fd2 are used to seize/dev/null pipelines at the same time.
2. CMD>/dev/null 2> & 1: Send stdout directly to/dev/null. After stderr inherits the fd1 pipeline, it is sent to/dev/null, /dev/null is opened only once, and only one pipeline fd1 is used, which includes stdout and stderr content. 3. in terms of Io efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing shell scripts, in many cases, we use cmd>/dev/null 2> & 1.