">/dev/null 2>&1" in the shell

Source: Internet
Author: User

Today in one of their technical group was asked such a question, and then the popular explanation, make a record, we see whether the explanation is clear!

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

The results of the command can be defined in% > to define the output

Decompose this combination: ">/dev/null 2>&1" is part of five.

Where 1:> represent redirects, such as echo "123" >/home/123.txt

2:/dev/null represents empty device files

3:2> indicates stderr standard error

4:& means equal to, 2>&1, indicating 2 output redirection equals 1

5:1 indicates 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 the title of this article is:

1>/dev/null: First of all, the standard output redirect to the empty device file, that is, not outputting any information to the terminal, plainly is not displaying any information.

2>&1: The standard error output is then redirected to the standard output, because the standard output is redirected to the empty device file, so the normal error output is redirected to the empty device file.

Is that clear, you understand?

By the way, tell the benefits of this use!

The most common ways are:

Command > File 2>file with command > file 2>&1

Do they have any different places?

First command > file 2>file means that the standard output information generated by the command and the wrong output information are sent to file. command > File 2>file this way, stdout and stderr are sent directly to FI. Le, file will be opened two times, so that stdout and stderr will cover each other, which writes quite a lot of FD1 and FD2 two of the pipes to preempt file at the same time.

and command >file 2>&1 This order will stdout directly to file, StdErr inherited the FD1 pipeline, and then sent to file, at this time, file was only opened once, also only used a pipeline FD1, It includes the contents of stdout and stderr.

In terms of IO efficiency, the previous command is less efficient than the one in the latter, so when writing a shell script, more often than not, we will write the command > file 2>&1.

This article is from the "Technology Achievement Dream" blog, please be sure to keep this source http://ixdba.blog.51cto.com/2895551/526442

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.