Linux Shell 1>/dev/null 2> & 1 meaning

Source: Internet
Author: User

Shell may often see: Echo log>/dev/null 2> & 1


Command results can be defined in the form of %>

/Dev/null: Indicates an empty device file.
>: Indicates the redirection location, for example, Echo "123">/home/123.txt
1: Indicates stdout standard output. The default value is 1. Therefore, ">/dev/null" is equivalent to "1>/dev/null"
2: Stderr standard error
&: Indicates equivalent, 2> & 1, indicating that 2's output redirection is equivalent to 1

1>/dev/null 2> & 1Statement description:
1>/dev/null: indicates that the standard output is redirected to an empty device file, that is, no information is output to the terminal. In other words, no information is displayed.
2> & 1: Next, standard error output redirection (equivalent to) standard output. Because the standard output has been redirected to the empty device file, the standard error output is also redirected to the empty device file.

Instance resolution:

CMD> A 2> A and CMD> A 2> & 1 why is it different?
CMD> A 2> A: Both stdout and stderr are directly sent to file a. File a is opened twice, which causes stdout and stderr to overwrite each other.
CMD> A 2> & 1: stdout is directly sent to file a. stderr is sent to file a After inheriting the fd1 pipeline. File a is opened only once, that is, it is opened by fd1.

The differences between the two are:

CMD> A 2> A is equivalent to two pipelines, fd1 and fd2, that compete with each other to use file;
CMD> A 2> & 1 only uses one pipeline fd1, but it already includes stdout and stderr.
In terms of Io efficiency, CMD> A 2> & 1 is more efficient.

You can often find the following command calls in some scripts, especially when calling crontab:

/Tmp/test. Sh>/tmp/test. log 2> & 1
The first half of/tmp/test. Sh>/tmp/test. log is easy to understand. What is the next 2> & 1?

To solve this problem, we still need to mention file redirection. We know that> and <are file redirection characters. So what are values 1 and 2?

In Shell, each process is associated with three system files: standard input stdin, standard output stdout, and standard error stderr. The file descriptors of the three system files are 0, 1, and 2, respectively. So here 2> & 1 means to output the standard error to the standard output.

The following uses an example to demonstrate the functions of 2> & 1:

$ CAT test. Sh
T
Date
Test. Sh contains two commands, T is a non-existent command, and an error is reported during execution. By default, the error is output to stderr. Date can be correctly executed and the time information is output. It is output to stdout by default.

./Test. Sh> test1.log
./Test. sh: Line 1: T: Command not found

$ Cat test1.log
Wed Jul 10 21:12:02 CST 2013


We can see that the execution result of date is redirected to the log file, and the error t cannot be executed is only printed on the screen.

$./Test. Sh> test2.log 2> & 1

$ Cat test2.log
./Test. sh: Line 1: T: Command not found
Tue Oct 9 20:53:44 CST 2007
This time, the contents of stderr and stdout are redirected to the log file.


In fact,> is equivalent to 1>, that is, redirection standard output, excluding standard errors. With 2> & 1, the standard error is redirected to the standard output, and then the standard output and standard error information are redirected together with the redirection. If you only want to redirect a standard error to a file, you can use 2> file.

In Linux Shell, "2> & 1" indicates that 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 error output is merged. 0 indicates that the keyboard input 1 indicates the screen output 2 indicates the error output, redirects the standard error to the standard output, and then drops it under/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)

Consider the file sharing structure generated by different dup2 () call sequences. See apue 3.10, 3.12

Reference recommendations:

1>/dev/null 2> & 1 meaning

/Dev/null 2> & 1 explanation

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.