In the shell, what does “ 2>&1 ” mean?

2018-12-31 02:49发布

In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command:

2>&1

So, if I want to use head on the output from g++, I can do something like this:

g++ lots_of_errors 2>&1 | head

so I can see only the first few errors.

I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick.

Can someone break this up and explain character by character what 2>&1 means?

15条回答
听够珍惜
2楼-- · 2018-12-31 03:15

That construct sends the standard error stream (stderr) to the current location of standard output (stdout) - this currency issue appears to have been neglected by the other answers.

You can redirect any output handle to another by using this method but it's most often used to channel stdout and stderr streams into a single stream for processing.

Some examples are:

# Look for ERROR string in both stdout and stderr.
foo 2>&1 | grep ERROR

# Run the less pager without stderr screwing up the output.
foo 2>&1 | less

# Send stdout/err to file (with append) and terminal.
foo 2>&1 |tee /dev/tty >>outfile

# Send stderr to normal location and stdout to file.
foo >outfile1 2>&1 >outfile2

Note that that last one will not direct stderr to outfile2 - it redirects it to what stdout was when the argument was encountered (outfile1) and then redirects stdout to outfile2.

This allows some pretty sophisticated trickery.

查看更多
怪性笑人.
3楼-- · 2018-12-31 03:15

Provided that /foo does not exist on your system and /tmp does…

$ ls -l /tmp /foo

will print the contents of /tmp and print an error message for /foo

$ ls -l /tmp /foo > /dev/null

will send the contents of /tmp to /dev/null and print an error message for /foo

$ ls -l /tmp /foo 1> /dev/null

will do exactly the same (note the 1)

$ ls -l /tmp /foo 2> /dev/null

will print the contents of /tmp and send the error message to /dev/null

$ ls -l /tmp /foo 1> /dev/null 2> /dev/null

will send both the listing as well as the error message to /dev/null

$ ls -l /tmp /foo > /dev/null 2> &1

is shorthand

查看更多
素衣白纱
4楼-- · 2018-12-31 03:17

Redirecting Input

Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.

The general format for redirecting input is:

[n]<word

Redirecting Output

Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.

The general format for redirecting output is:

[n]>word

Moving File Descriptors

The redirection operator,

[n]<&digit-

moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.

Similarly, the redirection operator

[n]>&digit-

moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.

Ref:

man bash

Type /^REDIRECT to locate to the redirection section, and learn more...

An online version is here: 3.6 Redirections

PS:

Lots of the time, man was the powerful tool to learn Linux.

查看更多
登录 后发表回答