in fact to understand the meaning of 2>&1, first of all should know that Linux has three kinds of standard input and output, respectively, Stdin,stdout,stderr, the corresponding number is 0,1,2. STDIN is the standard input, the default from the keyboard to read information; stdout is the standard output, the output output to the terminal by default, that is, the monitor and so on; stderr is the standard error message and is displayed on the terminal by default. Because stdout and stderr will be displayed by default on the terminal, in order to distinguish between the information, there is a number of 0,1,2 definition, with 1 to represent the stdout,2 stderr. Here's an example: Take the example in Rhce. 1, create a normal user test in the system, 2, log in as a normal user, or switch to normal user test with root login su-test; 3, executing the find/etc-name passwd command, by default outputs the command's execution results (STDOUT) and error messages (STDERR) to the terminal Monitor. 4, experience the role of number 1,2, Find/etc-name passwd >find.out 2> Find.err, the stdout and stderr will be stored in find.out and Find.err respectively, the command can also be written in the following three forms, you can understand 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 Find/etc-name passwd command's execution results output the correct output (STDOUT) received by 1, the wrong information (stdeRR) received by 2. 5, to display all the output and error messages, you can use & to represent all 1 and 2 of information, such as: 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 in the "2>1" such a train of thought, if according to the above wording, The system will default to redirect the wrong information (STDERR) 2 to a file with a name of 1, rather than to the desired (STDOUT). It is therefore necessary to add & differentiate. There is the use of 2>&1 , for example: find/etc-name passwd 2>&1 |less 7, and 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 to execute. 2>&1 |less indicates that the error message is redirected to standard output and is paginated with less.
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.