/dev/null 2>&1 Explain

  

with /dev/null 2>&1. This command means to redirect all standard output and error output to /dev/null. That is, discard all the information generated.

The following is for everyone, command > file 2>file is different from command > file 2>&1.

first command > file 2> file means to send the standard output information generated by the command, and the wrong output information to the file command command > file 2> file, stdout and stderr are sent directly to the file In this case, file will be opened twice, so stdout and stderr will overwrite each other, so that the FD1 and FD2 are used to grab the file at the same time.

and command >file 2>&1 This command sends stdout directly to the file. After stderr inherits the FD1 pipeline, it is sent to file. At this point, file is only opened once, and only one pipeline FD1 is used, which includes the contents of stdout and stderr. .

From the IO efficiency, the efficiency of the previous command is lower than that of the latter one, so it is compiled. When shell scripts, we will use command > file 2>&1 for more than one time.


About shell: >/dev/null 2>& ;1 Detailed

The shell may often see: >/dev/null 2>&1

The result of the command can be defined in the form of %>

Decompose this combination: “>/dev/null 2>&1” for five parts.

1:> Represents where to redirect, for example: echo "123" > /home/123.txt

2:/dev/null for empty device files

3:2> indicates stderr standard error

4:& means equivalent, 2>&1, indicating that 2 output redirection is equivalent to 1

5: 1 means stdout standard output, the system default value 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”

The statement execution process of this article title is:

1>/dev/null: first indicates the standard The output is redirected to an empty device file, that is, no information is output to the terminal. To put it plainly, no information is displayed.

2>&1: Next, the standard error output is redirected to standard output because the standard output has been redirected to an empty device file, so the standard error output is also redirected to the empty device file.



Copyright © Windows knowledge All Rights Reserved