Meaning of "2> & 1" in linux shell
Script:
Nohup/mnt/Nand3/H2000G>/dev/null 2> & 1 &
For & 1, it should be file descriptor 1, while 1 generally represents STDOUT_FILENO. In fact, this operation is a dup2 (2) Call. it outputs the standard output to all_result, and then copies the standard output to file descriptor 2 (STDERR_FILENO). The consequence is that file descriptors 1 and 2 point to the same file table item, it can also be said that the wrong output is merged. 0 indicates keyboard input 1 indicates screen output 2 indicates wrong output. redirects a standard error to the standard output and then throws it to/DEV/NULL. In layman's terms, all the standard output and standard errors are thrown into the waste bin.
Command> out. file 2> & 1 &
Command> out. file redirects the command output to the out. file. That is, the output content is not printed to the screen, but output to the out. file. 2> & 1 redirects a standard error to the standard output. The standard output has been redirected to the out. file, which outputs a standard error to the out. file. The last & is to run the command in the background.
Imagine what 2> 1 represents, 2 and> combined represents error redirection, and 1 represents an error redirected to a file 1, not a standard output;
Switch to 2> & 1, & combined with 1 indicates the standard output, and the error is redirected to the standard output.
Available
Ls 2> 1 test. The system does not report the error of NO 2 files, but outputs an empty file 1;
Ls xxx 2> 1 test, no errors in the xxx file are output to 1;
Ls xxx 2> & 1 test. The file 1 is not generated, but the error is returned to the standard output;
Ls xxx> out.txt 2> & 1, which can be changed to ls xxx 1> out.txt 2> & 1; the redirection symbol> timeout is a error and the output is transmitted to out.txt.
What 2> & 1 should be written below?
Command> file 2> & 1
First, the command> file redirects the standard output to the file. 2> & 1 indicates that the standard output is copied due to a standard error, that is, it is also redirected to the file, the final result is that both the standard output and errors are redirected to the file.
Command 2> & 1> file
2> & 1 the standard error copies the standard output, but the standard output is still on the terminal.> The output is redirected to the file, but the standard error is still on the terminal.
Use strace to see:
1. command> file 2> & 1
The key System Call Sequence for implementing redirection in this command is:
Open (file) = 3
Dup2 (3, 1)
Dup2 (1, 2)
2. command 2> & 1> file
The key System Call Sequence for implementing redirection in this command is:
Dup2 (1, 2)
Open (file) = 3
Dup2 (3, 1)