The shell can often be seen: >/dev/null 2>&1
/dev/null represents an empty device file
where does the > delegate redirect to, for example: echo "1111" >/home/a.txt
1 means stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 indicates stderr standard error
& means equivalent to, 2>&1, 2 output redirect equals 1
take a look at the statement in the title of this article:
1>/dev/null First indicates that the standard output is redirected to an empty device file, that is, does not output any information to the terminal
2>&1 Then, the standard error output redirection is equivalent to the standard output because the standard error output is redirected to the empty device file because the standard output was previously redirected to an empty device file.
we often see something like "2>&1″" in some scripts under Unix systems, such as "/path/to/prog 2>&1 >/dev/null &", what is the specific meaning of it?
Unix has several input and output streams that correspond to several numbers, namely: 0-Standard input stream (stdin), 1-standard output stream (stdout), 2-standard error stream (stderr). "2>&1″ means redirecting stderr to stdout and showing it together on the screen." If you do not add numbers, the default redirect action is for stdout (1), such as "ls-l > result" equivalent to "Ls-l 1 > result". This makes it easier for us to understand the redirection process more universally.
The following examples illustrate:
#cat std.sh
#!/bin/sh
echo "stdout"
echo "stderr" >&2
#/bin/sh std.sh 2>&1 >/dev/null
stderr
#/bin/sh std.sh >/dev/null 2>&1
The output of the first command is stderr, because stdout and stderr are redirected to/dev/null together, but stderr is not cleared, so it will still appear on the screen, and the second command has no output because when STDOUT is redirected to/dev/null , STDERR was redirected to stdout, so stderr was also exported to/dev/null.
Often seen in the shell: >/dev/null 2>&1