Meaning of "2>&1" in linux shell

export date_str=$(date "+%Y-%m-%d_%H%M%S")
echo $date_str
#pg_dump --host 202.105.182.135 --port 5432 --username "postgres" --role "postgres" --no-password  --format custom --blobs --verbose --file "/home/pandy/db_$date_str.bakup" "rhcpm_saas"
pg_dump --host 192.168.0.xxx --port 5432 --username "postgres" --role "postgres" --no-password  --format custom --blobs --verbose --file "/home/pandy/local_db_$date_str.bakup" "rhcpm_saas" >/home/pandy/test_out.file  2>&1
//Represents the standard output generated when the database is backed up, and redirects the output to the /home/pandy/test_out.file file.





Original:
http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.html

The script is:
      nohup /mnt/Nand3/H2000G >/dev/null 2>&1 &
      for & 1 more accurate It is said that it should be file descriptor 1, and 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 File descriptors 1 and 2 point to the same file table entry. It can also be said that the error 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 to / Go below 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 > 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 stands for standard output, will redirect errors to standard output.


      You can use
            ls 2>1 test, 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 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; 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,



dup2(1,2)
open(file) == 3
dup2(3,1)

      Consider what kind of file sharing structure different dup2() call sequences will produce.

Guess you like

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