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