Often seen in the shell: >/dev/null 2>&1

Source: Internet
Author: User
Tags add numbers

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.