In the linux shell, the meaning of "2>&1"

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. His standard output is to all_result

 

    , and then copy the 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.

 

   Where 0 means 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 to 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, 2 combined with > stands for error redirection, and 1 stands for error redirection to a file 1 instead of standard output;

 Replace it with 2>&1, and the combination of & and 1 represents standard output, and it becomes an error redirected to standard output.

 

     you can use it

 

     ls 2>1 test it, it will not report an error that there is no 2 file, but an empty file 1 will be output;

 

     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 goes to the standard output;

 

     ls xxx >out.txt 2>&1, can actually be replaced with ls xxx 1>out.txt 2>&1; the redirection symbol > defaults to 1, and errors and output are sent to out.txt.

 

   

 

 

    Why should 2>&1 be written at the back?

 

     command > file 2>&1

 

     The first is command > file to redirect 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 that standard output and error are redirected to file middle.

 

     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.

 

    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)

 

    Consider what kind of file-sharing structure different sequences of dup2() calls result in.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327076021&siteId=291194637