[Switch] "2>&1" meaning in linux shell

The script is: 
      nohup /mnt/Nand3/H2000G >/dev/null 2>&1 & 
      For & 1, it should be file descriptor 1 more accurately, and 1 generally represents STDOUT_FILENO. In fact, this operation is a dup2(2 ) call. He standard output to all_result, and then copied standard output to file descriptor 2 (STDERR_FILENO), the consequence is that file descriptors 1 and 2 point to the same file table entry, it can also be said that the wrong output is merged. Among them 0 Indicates that keyboard input 1 means screen output 2 means error output. Redirect standard error to standard output, and then throw it under /DEV/NULL. In layman's terms, it is to throw all standard output and standard error into the trash. 
      command >out.file 2>&1 & 
      command >out.file redirects the output of the command to the out.file file, that is, the output content is not printed on the screen, but is output to the out.file file. 2>&1 is to redirect the standard error to the standard output, where the standard output has been redirected to the out.file file, that is, the standard error is also output to the out.file file. The last & , is to let the command execute in the background. 
      Just imagine what 2>1 stands for, the combination of 2 and > represents error redirection, and 1 represents error redirection to a file 1 instead of standard output; 
replace it with 2>&1, and the combination of & and 1 represents standard output, It becomes an error redirected to standard output.

      You can  test
it with             ls 2>1 , it will not report the error that there is no file 2, but it will output an empty file 1;              ls xxx 2>1 test, the error without the file xxx is output to 1;              ls xxx 2> &1 test, the file 1 will not be generated, but the error will go to the standard output;              ls xxx >out.txt 2>&1, can actually be replaced with ls xxx 1>out.txt 2>&1; redirection symbol > default is 1, errors and output are passed to out.txt.       Why should 2>&1 be written at the back?       command > file 2>&1         First, command > file redirects standard output to file, 2>&1 is the behavior of standard error copying standard output, that is, it is also redirected to file, and the final result is standard output and error are redirected to file.       command 2>&1 >file        2>&1 Standard error duplicates the behavior of standard output, but at this time standard output is still on the terminal. The output is redirected to file after >file, but standard error remains on the terminal.







You can see with strace: 
1. command > file 2>&1 
The key system call sequence for 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 redirection in this command is: 
dup2(1,2) 
open(file) == 3 
dup2(3,1)

Reprinted from: http://blog.csdn.net/annicybc/article/details/4814872

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326388588&siteId=291194637