Explanation of the meaning of "2>&1″" in the Linux shell script

Source: Internet
Author: User

The script is: nohup/mnt/nand3/h2000g  >/dev/null  2>&1 &  for & 1 more accurately said file descriptor 1, and 1 The general Representative is Stdout_fileno, which is actually a dup2 (2) call. He outputs the standard output to All_result, and then copies the standard output to the file Descriptor 2 (Stderr_fileno), The consequence is that the file descriptors 1 and 2 point to the same file table entry, or the wrong output is merged. where 0 means keyboard input 1 means that the screen output 2 indicates an error output. REDIRECT standard error to standard output, then throw it under/dev/null. In layman's words, all standard output and standard errors are thrown into the trash.  # command >out.file  2>&1 & command >out.file is to redirect the command output to the Out.file file, that is, the output is not printed to the screen, but is output to the Out.file file. 2>&1 is redirecting standard errors to standard output, where the standard output has been redirected to the Out.file file, and the standard error is output to the Out.file file. The last & is to have the command execute in the background.   Imagine what 2>1 stands for, 2 and > combined for error redirection, and 1 for error redirection to a file 1 instead of standard output; replacing 2>&1,& with 1 represents standard output, which becomes error redirection to standard output.   You 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, actually can be replaced by ls xxx 1>out.txt 2>&1; redirect symbol > default is 1, Errors and outputs are uploaded to the out.txt.   Why 2>&1 to write in the back?  # command > File 2>&1  First is command > file redirect standard output to file, 2>&1 is the standard error copy of the standard output behavior, which is also redirected to file, the end result is that the standard output and errors are redirected to file. The  # command 2>&1 >file 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.   can be seen with Strace:  1. Command > File 2>&1 the key system call sequence that implements redirection in this order is: open (file) = = 3dup2 (3,1) dup2 ( 2). Command 2>&1 >file The key system call sequence that implements the redirect is: Dup2 () open (file) = = 3dup2 (3,1)  

Meaning of 2>&1″ in the Linux shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.