The meaning of "2>&1" in the Linux shell
Script:
nohup/mnt/nand3/h2000g >/dev/null 2>&1 &
For & 1 It is more accurate to say that the file descriptor is 1, and 1 generally represents stdout_fileno, which is actually a dup2 (2) call. He standard output to All_result, then copy standard output to file descriptor 2 (stderr_ Fileno), the consequence is that file descriptors 1 and 2 point to the same file table entry, or that the wrong output is merged. 0 indicates that keyboard input 1 indicates that screen output 2 represents the error output. REDIRECT standard error to standard output and throw it under/dev/null. In layman's parlance, all standard output and standard errors are thrown into the dustbin.
Command >out.file 2>&1 &
Command >out.file redirects the command's output to the Out.file file, where the output does not print to the screen, but is output to the Out.file file. 2>&1 is to redirect standard error to standard output, where the standard output has been redirected to the Out.file file, and the standard error is also exported to the Out.file file. The last & is to have the command run in the background.
Imagine what 2>1 stands for, 2 and > combine to represent error redirection, while 1 represents error redirection to a file 1, not standard output;
Replacing 2>&1,& with 1 means standard output, which turns error redirection to standard output.
can use
LS 2>1 test, will not report No 2 file errors, but will output an empty file 1;
ls xxx 2>1 test, no xxx This file error output to 1;
ls xxx 2>&1 test, will not generate 1 of this file, but the error ran to the standard output;
ls xxx >out.txt 2>&1, in fact, can be replaced by ls xxx 1>out.txt 2>&1; redirection symbol > The default is 1, error and output are transmitted to the out.txt.
What 2>&1 to write in the back?
Command > File 2>&1
First, command > file redirects standard output to file, 2>&1 is the standard error copy of the standard output behavior, which is also redirected to file, and the final result is that standard output and errors are redirected to file.
Command 2>&1 >file
The 2>&1 standard error copies the behavior of the standard output, but at this point the standard output is still in the terminal. The output is redirected to file after >file, but the standard error remains at the terminal.
With Strace you can see:
1. Command > File 2>&1
The key system call sequence that implements redirection in this command is:
Open (file) = = 3
Dup2 (3,1)
Dup2 (1,2)
2. Command 2>&1 >file
The key system call sequence that implements redirection in this command is:
Dup2 (1,2)
Open (file) = = 3
Dup2 (3,1)