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

Source: Internet
Author: User

[From] http://www.ixdba.net/a/ OS /linux/2010/0422/35.html

 

Shell may often see:>/dev/null 2> & 1

Command results can be defined in the form of %>

Break down this combination: ">/dev/null 2> & 1" is five parts.

1:> indicates the redirection location, for example, echo "123">/home/123.txt
2:/dev/null indicates that the device file is empty.
> Indicates stderr standard error
4: & indicates equivalent meaning, 2> & 1, indicating that 2's output redirection is equivalent to 1
5:1 indicates stdout standard output. The default value is 1. Therefore, ">/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 in the title of this article is as follows:
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: Next, the standard error output is redirected 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.

Are you sure you want to understand it!

 

By the way, we will compare the advantages of this application!

The most common methods are:

Command> file 2> file and command> file 2> & 1

 

What are their differences?

First, command> file 2> file means to send the standard output information and error output information generated by the command to file. command> file 2> file: Both stdout and stderr are directly sent to file. The file will be opened twice, so that stdout and stderr will overwrite each other, in this way, both FD1 and FD2 are used to seize the file pipeline at the same time.
Command> file 2> & 1 directly sends stdout to file. After stderr inherits the FD1 pipeline, it is sent to file. At this time, the file is opened only once, only one pipeline FD1 is used, which includes stdout and stderr content.
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 most cases, we write commands> file 2> & 1.

 

This article from fengman.net (www.ixdba.net) original link: http://www.ixdba.net/a/ OS /linux/2010/0422/35.html

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.