&>/Dev/null is equivalent to>/dev/null 2> & 1

Source: Internet
Author: User

In Cron, everyone prefers to write command &>/dev/null.
I have been wondering what it means. First, the "&" in it does not mean running in the background. cron does not need to write back-end operations. He found the following information, "&>/dev/null" is short for ">/dev/null 2> & 1. The latter's explanation is detailed in the reposted Article below ~~ Note that "> file 2> & 1" and "2> & 1> file" have different meanings.
Reprinted: http://www.diybl.com/course/3_program/shell/shelljs/2008109/149623.html
The script is:
Nohup/mnt/nand3/h2000g>/dev/null 2> & 1 &
For & 1, it should be file descriptor 1, while 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 that file descriptors 1 and 2 point to the same file table item, it can also be said that the wrong output is merged. 0 indicates keyboard input 1 indicates screen output 2 indicates wrong output. redirects a standard error to the standard output and then throws it to/dev/null. In layman's terms, all the standard output and standard errors are thrown into the waste bin.
Command> out. File 2> & 1 &
Command> out. File redirects the command output to the out. File file. That is, the output content is not printed to the screen, but output to the out. File file. 2> & 1 redirects a standard error to the standard output. The standard output has been redirected to the out. File file, which outputs a standard error to the out. File file. The last & is to run the command in the background.

Imagine what 2> 1 represents, 2 and> combined represents error redirection, and 1 represents an error redirected to a file 1, not a standard output;
Switch to 2> & 1, & combined with 1 indicates the standard output, and the error is redirected to the standard output.
You can use
Ls 2> 1 test. The system does not report the error of NO 2 files, but outputs an empty file 1;
Ls XXX 2> 1 test, no errors in the xxx file are output to 1;
Ls XXX 2> & 1 test. The file 1 is not generated, but the error is returned to the standard output;
Ls XXX> out.txt 2> & 1, which can be changed to LS xxx 1> out.txt 2> & 1; the redirection symbol> timeout is a error and the output is transmitted to out.txt.
Why 2> & 1 should be written at the end?
Command> file 2> & 1
First, the command> file redirects the standard output to the file. 2> & 1 indicates that the standard output is copied due to a standard error, that is, it is also redirected to the file, the final result is that both the standard output and errors are redirected to the file.
Command 2> & 1> File
2> & 1 the standard error copies the standard output, but the standard output is still on the terminal.> The output is redirected to the file, but the standard error is still on the terminal.
Use strace to see:
1. Command> file 2> & 1
The key System Call Sequence for implementing 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 implementing redirection in this command is:
Dup2 (1, 2)
Open (File) = 3

Dup2 (3, 1)

From: http://hi.baidu.com/kechen_linux/item/c630acc5bf700d13b67a24a1

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.