The 2>&1 of Linux

Source: Internet
Author: User
Tags stdin

Reprinted from: Http://blog.sina.com.cn/s/blog_5842daa30101enz5.html often concerned about the Linux script people, must have seen 2>&1 such usage, At first it must not understand the meaning and why it is such a combination. Yesterday happened to see this 2>&1 again, so determined to understand the meaning of the.      in fact, to understand the meaning of 2>&1, we should first know that there are three kinds of standard input and output in Linux, respectively, is Stdin,stdout,stderr, the corresponding number is 0,1,2. STDIN is the standard input, read the information by default from the keyboard, stdout is the standard output, by default output output to the terminal, that is, the display and so on, stderr is a standard error message, the default will be displayed on the terminal. 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.      Here's an example: Take the example in Rhce.     1, create a normal user test;    2 in the system, log in as a normal user, or switch to normal user test with root login su-test;     3, executes the find/etc-name passwd command, and by default outputs the command's execution result (STDOUT) and error message (STDERR) to the terminal display.     4, experience the role of numbering, Find/etc-name passwd >find.out 2> Find.err, here will be stdout and stderr respectively to Find.out and Find.err, the command can also be written in the following three forms, you can appreciate the meaning of the number.     find/etc-name passwd 1>find.out 2>find.err    find/etc-name Passwd 2>find.err >find.out    find/etc-name Passwd 2>find.err 1>find.out     My personal understanding is Find/etc-name The execution result of the passwd command outputs the correct output (STDOUT) received by 1, and the wrong information (STDERR) is received by 2.     5, to display all output and error messages, you can use & to represent all 1 and 2 information, for example:     find/etc-name passwd &>find.all    6, sometimes want to redirect the wrong information to the output, that is, 2 of the results redirected to 1 there is "2>1" such ideas, if according to the above-mentioned wording, The error message (STDERR) 2 is redirected to a file with the name 1 instead of the one you want (STDOUT) by default. It is therefore necessary to add & to differentiate. With the use of 2>&1 , for example:     find/etc-name passwd 2>&1 |less     7, 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 execution.     2>&1 |less     indicates that the error message is redirected to standard output and is paginated with less.   People who are always interested in Linux scripts must have seen the usage of 2>&1, and at first must not understand the meaning and why it is such a combination. Yesterday happened to see this 2>&1 again, so determined to understand the meaning of the.      actually need to figure out 2>&1 the meaning, first should know that there are three kinds of standard input and output in Linux, respectively, is Stdin,stdout,stderr, the corresponding number is 0,1,2. STDIN is the standard input, read the information by default from the keyboard, stdout is the standard output, by default output output to the terminal, that is, the display and so on, stderr is a standard error message, the default will be displayed on the terminal. 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.      Here's an example: Take the example in Rhce.     1, create a normal user test;    2 in the system, log in as a normal user, or switch to normal user test with root login su-test;     3, executes the find/etc-name passwd command, and by default outputs the command's execution result (STDOUT) and error message (STDERR) to the terminal display.     4, experience the role of numbering, Find/etc-name passwd >find.out 2> Find.err, here will be stdout and stderr respectively to Find.out and Find.err, the command can also be written in the following three forms, you can appreciate the meaning of the number.     find/etc-name passwd 1>find.out 2>find.err    find/etc-name Passwd 2>find.err >find.out    find/etc-name Passwd 2>find.err 1> find.out     my 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.     5, to put all theThe output and error messages are displayed and can be used & to represent all 1 and 2 information, for example:     find/etc-name passwd &>find.all     6, sometimes want to redirect the wrong information to the output, that is, 2 of the results are redirected 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 what is thought (STDOUT). It is therefore necessary to add & to differentiate. With the use of 2>&1 , for example:     find/etc-name passwd 2>&1 |less     7, 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 execution.     2>&1 |less     indicates that the error message is redirected to standard output and is paginated with less.

Linux 2>&1

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.