Input and output redirection in a Linux Shell environment, denoted by symbols < and >. 0, 1, and 2 represent standard inputs, standard outputs, and standard errors, respectively.
1. redirect standard output to file: Cat fo > foo.txt2. REDIRECT standard error to file cat fo 2> foo.txt3. REDIRECT standard output to standard error cat fo 1>&24. REDIRECT standard error to standard output cat fo 2& gt;&15. REDIRECT standard output, standard error to the same file cat fo > Fo.txt 2>&1 or cat foo &> Foo.txt the first order here is important, redirect the standard output to a file, and then output the standard error to the standard output, because the standard output has been redirected to the file, so the standard error is redirected to the file. >& Same as &> effect--------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------
I used to see & 1, &2 such symbols, but I don't know what it means.
There are 12 file descriptors in the system, 0,1,2 is standard input, output and error. You can use file descriptors 3 to 9 arbitrarily
file |
File Descriptor |
Input file--standard input |
0 |
Output file-standard output |
1 |
Error output file-standard error |
2 |
Standard input: 0
The default is keyboard input, or it can be the output of a file or other command
Standard output: 1
The default is output to the terminal, you can also output to a file
Standard ERROR: 2
Command error message output, default output to terminal, can also output to file
If the file descriptor is not specifically specified, the command will use the default file descriptor, keyboard output, terminal output
After you enter a cmd, if no standard output or error output is specified, all output is displayed on the screen,
If the command is: cmd > result.out the command is equivalent to CMD 1>result.out, the standard output is redirected to the Result.out file, and the error output is displayed on the screen;
If the command is: cmd 2>result.out, the error output is redirected to the Result.out file, and the standard output is displayed on the screen;
If the command is: cmd >result.out 2>&1, both the standard output and the error output are redirected to the Result.out file, and there is no output on the screen.
Input and output redirection in the Shell environment