What exactly is 2>&1 in Linux __linux

Source: Internet
Author: User

Original connection: http://blog.csdn.net/ggxiaobai/article/details/53507530

---------------------------------------------------------


We often encounter Nohup command>/dev/null 2>&1 & such forms of command under Linux. First of all, we'll probably break down this command. First, a nohup represents the current user and the system under the incoming call to ignore the response hup message. & runs the command in the form of a job in the background. Then left Command>/dev/null 2>&1,command>/dev/null better understand,/dev/null means an empty device, that is, the command's execution results are redirected to an empty device, The plain thing is not to display any information. So what does 2>&1 mean? 2>&1 several basic symbols and their meanings /dev/null represents an empty device file 0 indicates stdin standard input 1 indicates stdout standard output 2 indicates stderr standard error from Command>/dev/null.

In fact, this command is an abbreviated version, for a redirection command, it must be a > B this form, then command >/dev/null is the command to act as a role,/dev/null role as B. This looks more reasonable, in fact, a command must not act as a, must be the command to execute the output produced to act as a, in fact, is the standard output stdout. So command >/dev/null is equivalent to executing command 1 >/dev/null. The Execute command produces the standard output stdout (in 1) and redirects to the/dev/null device file. Tell me about 2>&1 .

With command >/dev/null equivalent to command 1 >/dev/null, the 2>&1 is well understood, 2 is standard error, 1 is standard output, So this command is not the equivalent of redirecting standard errors to standard output. Wait, it's &1 instead of 1, what's & here. Here & is equivalent to standard output. This is a bit difficult to understand, first look below. the difference between Command>a 2>a and Command>a 2>&1

The above analysis, for Command>a 2>&1, is equivalent to command 1>a 2>&1 can understand that the standard input generated by command execution is redirected to file A, and the standard error is redirected to file a. So is it said that command 1>a 2>&1 is equivalent to command 1>a 2>a. In fact, command 1>a 2>&1 is different from command 1>a 2>a, except that it only opens once file a, which opens the file two times and causes stdout to be overwritten by stderr. The meaning of the &1 can be understood as a reference to the standard output, which refers to the redirected standard output to produce an open a. In terms of IO efficiency, command 1>a 2>&1 is more efficient than command 1>a 2>a. give me a chestnut .

A shell.

test.sh
#!/bin/sh
t
date

chmod +x test.sh increases execution permissions for test.sh. Here we have two commands, where the T-command does not exist, execution will error, output to stderr. Date is performed correctly, and execution outputs the current time and is output to stdout.

Execution./test.sh > Res1.log results are

We found that stderr was not redirected to Res1.log, and stderr was printed on the screen. This is further proof of what is said above./test.sh > Res1.log equivalent to./test.sh 1>res1.log

Execution./test.sh>res2.log 2>&1 result is

This time we found that stdout and stderr were redirected to the Res2.log. The above we did not to stderr is 2 instructions how to output, stderr output to the screen, here we not only to stdout, redirect to Res2.log, the standard error is also described, let it redirect to the Res2.log reference is Res2.log the file descriptor. think again .

Why 2>&1 to write in the back of Command>1, directly with 2 can do. such as LS 2>a. In fact, this usage is also possible, the LS command lists the current directory, with stdout (1), because this time there is no stderr (2), this time the execution of LS 2>a will normally produce a file of a, but a file is empty, Because the execution of LS does not produce stderr (2).



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.