The 2>&1 in Linux

Source: Internet
Author: User

We often encounter nohup command>/dev/null 2>&1 & commands in the form of Linux. First of all, we roughly decompose this command. The first is a nohup to indicate that the current user and the system's reply to the city ignores the response HUP message. & Is to run the command as a background job. Then there is command>/dev/null 2>&1, command>/dev/null is better understood, /dev/null means an empty device, that is to say, the execution result of the command is redirected to the empty device. No information is displayed. So what does 2>&1 mean?

2>&1 Several basic symbols and their meanings
/dev/null means empty device file
0 means stdin standard input
1 means stdout standard output
2 means stderr standard error
Speaking from command>/dev/null
In fact, this command is an abbreviated version. For a redirect command, it must be in the form of a> b. Then, command> /dev/null is that command plays the role of a, and /dev/null plays the role of b. This seems reasonable. In fact, a command must not act as a, and the output generated by the command execution must act as a. In fact, it is the standard output stdout. So command> /dev/null is equivalent to executing command 1> /dev/null. Executing command produces standard output stdout (denoted by 1), which is redirected to the device file of /dev/null.

Talk 2>&1
Through the above command> /dev/null is equivalent to command 1> /dev/null, then 2>&1 is also easy to understand, 2 is standard error, 1 is standard output, then this command is not equivalent to the standard Is the error redirected to standard output? Wait for &1 instead of 1, what is & here? Here & is equivalent to the standard output. It's a bit difficult to understand here, first look below.

The difference between command>a 2>a and command>a 2>&1
Through the above analysis, for command>a 2>&1 this command is equivalent to command 1>a 2>&1. It can be understood that the standard input generated by executing the command is redirected to file a, and the standard error is also redirected to file a in. So does it mean that command 1>a 2>&1 is equivalent to command 1>a 2>a. Actually not, there is a difference between command 1>a 2>&1 and command 1>a 2>a, the difference is that the former only opens the file a once, the latter will open the file twice, and causes stdout to be overwritten by stderr. The meaning of &1 can be understood as a reference to standard output, which refers to redirecting standard output to generate an open a. In terms of IO efficiency, command 1>a 2>&1 is more efficient than command 1>a 2>a.

Give a chestnut
Come a shell

//test.sh
#!/bin/sh
t
date
chmod +x test.sh adds execute permission to test.sh. Here we got two commands, in which the t instruction does not exist, an error will be reported during execution, and it will be output to stderr. The date can be executed normally, and the execution will output the current time and output to stdout.

The execution of ./test.sh> res1.log results in

We found that stderr was not redirected to res1.log, stderr was printed on the screen. This further proves that the above./test.sh> res1.log is equivalent to ./test.sh 1>res1.log

Execute ./test.sh>res2.log 2>&1 and the result is

This time we found that both stdout and stderr were redirected to res2.log. In the above we did not explain how to output stderr, which is 2, stderr is output to the screen. Here we not only explain stdout, redirect to res2.log, but also explain the standard error, let it be redirected to res2. The log reference is the file descriptor of res2.log.

Think again
Why 2>&1 should be written after command>1, just use 2 directly. For example, ls 2>a. In fact, this usage is also possible. The ls command lists the current directory, which is indicated by stdout(1). Since there is no stderr(2) at this time, executing ls 2>a will also produce a file normally, but a The file is empty, because the execution of ls at this time did not produce stderr(2).

2>&1 in Linux

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.