/Dev/null 2> & 1 basic explanation

Source: Internet
Author: User

Stdou and stderr are discarded.

In Shell, each process is associated with three system files: stdin, stdout, and stderr. The file descriptors of the three system files are 0, 1 and 2.

0-stdin standard input

1-stdout standard output

2-stderr error output

Use/dev/null 2> & 1. this command redirects all standard output and error output to/dev/null, that is, it discards all generated information.

   Let's take a look at the following,Command> File2> File AndCommand>File 2> & 1What's the difference.

          First Command>File 2> FileThe standard output information generated by the Command and the wrong output information are sent to the file. Command> File 2> File  In this way, both stdout and stderr are directly sent to the file,
The file will be opened twice, so that stdout and stderr will overwrite each other, so that the fd1 and fd2 are used to seize the file pipeline at the same time.          While Command> file 2> & 1This command directly sends stdout to file. After stderr inherits the fd1 pipeline, it is sent to file. At this time, file
Only once opened, and only one pipeline fd1 is used, which includes stdout and stderr contents.          In terms of Io efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing shell scripts Command> file 2> & 1This method is used.  Shell:>/dev/null 2> & 1 Details

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
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.

Other references


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.