Shell's ">/dev/null 2>&1"

Source: Internet
Author: User

Today in one of their own technical group was asked such a question, and then a popular explanation, make a record, we look at the explanation is clear!
The shell can often be seen: >/dev/null 2>&1
The result of the command can be defined in the form of a%> output
Break down this combination: ">/dev/null 2>&1" is part five.
Where does the 1:> delegate redirect to, for example: echo "123" >/home/123.txt
2:/dev/null represents an empty device file
3:2> indicates stderr standard error
4:& means equal to, 2>&1, 2 output redirect equals 1
5:1 means stdout standard output, the system default 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"
Then the statement execution procedure for this article title is:
1>/dev/null: First of all, the standard output redirects to the empty device file, that is, do not output any information to the terminal, it is plainly not to display any information.
2>&1: The standard error output is then redirected to 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.
Are you clear, everybody understand?
By the way, the benefits of this use are described!
The most commonly used methods are:
Command > File 2>file with command > file 2>&1
Is there anything different about them?


The first command > file 2>file means to send the standard output information generated by the commands to file with the wrong output information. Command > File 2>file StdOut and stderr are sent directly to file, file will be opened two times, so stdout and stderr will cover each other, so write quite use FD1 and FD2 two simultaneously to seize the file of the pipeline.
and command >file 2>&1 This order will stdout directly sent to file, stderr inherit the FD1 pipeline, and then sent to file, at this time, file was opened only once, also only used a pipeline FD1, It includes the contents of stdout and stderr.
From IO efficiency, the efficiency of the previous command is less efficient than the one in the following command, so when writing a shell script, we will command > file 2>&1.

Shell's ">/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.