The meaning of Linux 2>&1

Source: Internet
Author: User
Tags stdin

2>&1 means that standard error (2) is also directed to the output file of standard output (1).

We come to know the following: three standard input and output of Linux, respectively, is Stdin,stdout,stderr, the corresponding number is 0,1,2.

STDIN is the standard input, read the information by default from the keyboard, stdout is the standard output, by default output output to the terminal, that is, the display and so on, stderr is a standard error message, the default will be displayed on the terminal. Since stdout and stderr are displayed by default on the terminal, in order to distinguish between the information, there is the definition of the number of 0,1,2, with 1 means that stdout,2 represents stderr.

From Command>/dev/null.

In fact, this command is an abbreviated version, for a redirect command, this is definitely the a > b form, then it is the command to command > /dev/null act as a role,/dev/null act as a role of B. This seems reasonable, in fact, a command must not be able to act as a, it is definitely command execution output to act as a, in fact, is the standard output stdout. So command > /dev/null the equivalent is executed command 1 > /dev/null . The execution command produces the standard output stdout (denoted by 1) and redirects to the/dev/null device file.

Talk about 2>&1.

By command > /dev/null the above equivalence command 1 > /dev/null , then for the 2>&1 good understanding, 2 is the standard error, 1 is the standard output, then this command is equivalent to the standard error redirection to the standard output. Wait, it's &1, not 1. What's & here? This is equivalent & to the standard output . This is a bit difficult to understand, first look at the following.

The difference between Command>a 2>a and Command>a 2>&1

The above analysis, for command>a 2>&1 This command, is equivalent to the command 1>a 2>&1 standard input that can be understood as the execution command is redirected to file A, and the standard error is redirected to file a. Is it command 1>a 2>&1 equivalent to saying so command 1>a 2>a ? Actually not, command 1>a 2>&1 and command 1>a 2>a still have a difference, the difference is that the former only open a file A, the latter will open the file two times, and cause stdout to be stderr covered. &1 can be understood as a reference to a standard output, and the reference is to redirect the standard output to produce an open a. Higher command 1>a 2>&1 efficiency than IO efficiency command 1>a 2>a

Think again.

Why did 2>&1 put it behind? My understanding is that because 2 (that is, the error output) is redirected to &1, which is the standard output reference, which is the standard output open file, you need to open the 1 output file in front of you now.

Nohup and Nohup.out

If you use Nohup to execute a command with &, you can execute the command to the background without hanging up, and if you do not specify a redirect file, the output will be appended to the Nohup.out file in the current directory. If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out file, as shown in the following example:

Nohup test.pl 2>&1 &

The meaning of Linux 2>&1

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.