2>&1 usage instructions in the Linux shell

Source: Internet
Author: User
Tags stdin

There are three standard input and output in Linux, namely Stdin,stdout,stderr, the corresponding number is 0,1,2.

STDIN is a standard input that reads information from the keyboard by default;
STDOUT is the standard output, by default output output to the terminal, that is, the display and other things;
STDERR is a standard error message and is also displayed on the terminal by default.
Since stdout and stderr are displayed by default on the terminal, in order to distinguish between the information, there is the definition of the number of 0,1,2, with 1 means that stdout,2 represents stderr.

The following examples illustrate:

Log in as a normal user (Cent OS 7), execute the find/etc-name passwd command, the default will be the command execution results (STDOUT) and error messages (STDERR) are output to the terminal display.

Experience the role of numbering, Find/etc-name passwd 1>find.out 2>find.err, where STDOUT and STDERR are stored in find.out and Find.err respectively

The personal understanding is that the execution of the Find/etc-name passwd command outputs the correct output (STDOUT) received by 1, and the wrong information (STDERR) is received by 2.

To display all output and error messages, you can use & to represent all 1 and 2 of the information, for example:
Find/etc-name passwd &>find.all

Sometimes you want to redirect the wrong information to the output, that is, to redirect 2 of the results to 1 in the "2>1" the idea, if you follow the above, the system will default to the wrong information (STDERR) 2 is redirected to a file named 1, rather than the thought (STDOUT). It is therefore necessary to add & to differentiate. There is the use of 2>&1:

Find/etc-name passwd 2>&1 |less

Sometimes you can see this usage:
Find/etc-name passwd &2>&1 | Less
This can be broken down into
Find/etc-name passwd & indicates that the previous command was placed in the background.
2>&1 | Less means redirecting error messages to standard output and using less for paging.

2>&1 usage instructions in the Linux shell

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.