要将stderr和stdout合并到stdout流中,我们将其附加到命令中:

2>&1

例如,查看编译g++main.cpp时的前几个错误:

g++ main.cpp 2>&1 | head

2>&1的详细含义是什么?


当前回答

0表示输入,1表示标准输出,2表示标准错误。

一个提示:somecmd>1.text 2>&1是正确的,而somecmd2>&1>1.text完全错误,没有效果!

其他回答

unix命令2>&1

这用于将错误打印到终端。

当产生错误时,它们将被写入内存地址&2的“标准错误”缓冲区,以及来自该缓冲区的2个引用和流。当产生输出时,它们被写入“标准输出”缓冲区的内存地址&1,以及来自该缓冲区的1个引用和流。

所以回到指挥部。每当程序unix_commands产生错误时,它都会将其写入错误缓冲区。因此,我们创建一个指向缓冲区2的指针,并将>错误重定向到输出缓冲区&1。此时我们就完成了,因为输出缓冲区中的任何内容都由终端读取和打印。

该构造将标准错误流(stderr)发送到标准输出(stdout)的当前位置——其他答案似乎忽略了这个货币问题。

您可以使用此方法将任何输出句柄重定向到另一个,但它最常用于将stdout和stderr流引导到单个流中进行处理。

例如:

# 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

请注意,最后一个命令不会将stderr重定向到outfile2,而是将其重定向到遇到参数时的stdout(outfile1),然后将stdout重定向到outile2。

这允许一些相当复杂的诡计。

回答您的问题:它接受任何错误输出(通常发送到stderr)并将其写入标准输出(stdout)。

例如,当您需要为所有输出分页时,这对于“more”很有用。有些程序喜欢将使用信息打印到stderr中。

帮助你记住

1=标准输出(程序打印正常输出)2=标准错误(程序打印错误)

“2>&1”只是将发送到stderr的所有内容指向stdout。

我还建议阅读这篇关于错误重定向的文章,其中详细介绍了这个主题。

关于重定向的一些技巧

这方面的某些语法特殊性可能具有重要的行为。这里有一些关于重定向、STDERR、STDOUT和参数排序的小示例。

1-覆盖还是附加?

符号>表示重定向。

>意味着作为一个完整的文件发送到,如果存在则覆盖目标(请参阅后面的noclobber bash特性)。>>表示除了发送之外,如果存在,将附加到目标。

在任何情况下,如果文件不存在,将创建该文件。

2-shell命令行依赖于顺序!!

为了测试这一点,我们需要一个简单的命令,它将在两个输出上发送一些信息:

$ ls -ld /tmp /tnt
ls: cannot access /tnt: No such file or directory
drwxrwxrwt 118 root root 196608 Jan  7 11:49 /tmp

$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt 2>/dev/null
drwxrwxrwt 118 root root 196608 Jan  7 11:49 /tmp

(当然,您应该没有名为/tnt的目录;)。好吧,我们成功了!!

那么,让我们看看:

$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt >/dev/null 2>&1

$ ls -ld /tmp /tnt 2>&1 >/dev/null
ls: cannot access /tnt: No such file or directory

最后一个命令行将STDERR转储到控制台,这似乎不是预期的行为。。。但是

如果要对标准输出、错误输出或两者进行一些后置过滤:

$ ls -ld /tmp /tnt | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory
<-- drwxrwxrwt 118 root root 196608 Jan  7 12:02 /tmp --->

$ ls -ld /tmp /tnt 2>&1 | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->
<-- drwxrwxrwt 118 root root 196608 Jan  7 12:02 /tmp --->

$ ls -ld /tmp /tnt >/dev/null | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt >/dev/null 2>&1 | sed 's/^.*$/<-- & --->/'

$ ls -ld /tmp /tnt 2>&1 >/dev/null | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->

请注意,本段中的最后一个命令行与前一段中的完全相同,我在其中所写的似乎不是预期的行为(因此,这甚至可能是预期的行为)。

嗯,关于重定向有一些小技巧,可以在两个输出上执行不同的操作:

$ ( ls -ld /tmp /tnt | sed 's/^/O: /' >&9 ) 9>&2  2>&1  | sed 's/^/E: /'
O: drwxrwxrwt 118 root root 196608 Jan  7 12:13 /tmp
E: ls: cannot access /tnt: No such file or directory

注意:&9描述符会自发出现,因为)9>&2。

附录:nota!在新版本的bash(>4.0)中,有一个新功能和更性感的语法来完成这类任务:

$ ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /')
O: drwxrwxrwt 17 root root 28672 Nov  5 23:00 /tmp
E: ls: cannot access /tnt: No such file or directory

最后,对于这种级联输出格式:

$ ((ls -ld /tmp /tnt |sed 's/^/O: /' >&9 ) 2>&1 |sed 's/^/E: /') 9>&1| cat -n
     1  O: drwxrwxrwt 118 root root 196608 Jan  7 12:29 /tmp
     2  E: ls: cannot access /tnt: No such file or directory

附录:nota!两种方式都有相同的新语法:

$ cat -n <(ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /'))
     1  O: drwxrwxrwt 17 root root 28672 Nov  5 23:00 /tmp
     2  E: ls: cannot access /tnt: No such file or directory

STDOUT通过特定的过滤器,STDERR到另一个,最后合并的两个输出通过第三个命令过滤器。

2b-改用|&

语法命令|&。。。可以用作命令2>&1|…的别名。。。。关于命令行顺序的规则也适用。更多详细信息,请参见bash中运算符|&的含义是什么?

3-一个关于noclobber选项和>|语法的单词

这是关于覆盖:

虽然set-o noclobber指示bash不要覆盖任何现有文件,但>|语法允许您通过以下限制:

$ testfile=$(mktemp /tmp/testNoClobberDate-XXXXXX)

$ date > $testfile ; cat $testfile
Mon Jan  7 13:18:15 CET 2013

$ date > $testfile ; cat $testfile
Mon Jan  7 13:18:19 CET 2013

$ date > $testfile ; cat $testfile
Mon Jan  7 13:18:21 CET 2013

文件每次都会被覆盖,现在:

$ set -o noclobber

$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan  7 13:18:21 CET 2013

$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan  7 13:18:21 CET 2013

通过>|:

$ date >| $testfile ; cat $testfile
Mon Jan  7 13:18:58 CET 2013

$ date >| $testfile ; cat $testfile
Mon Jan  7 13:19:01 CET 2013

取消设置此选项和/或查询(如果已设置)。

$ set -o | grep noclobber
noclobber           on

$ set +o noclobber

$ set -o | grep noclobber
noclobber           off

$ date > $testfile ; cat $testfile
Mon Jan  7 13:24:27 CET 2013

$ rm $testfile

4-最后一个技巧和更多。。。

对于重定向给定命令的两个输出,我们看到正确的语法可以是:

$ ls -ld /tmp /tnt >/dev/null 2>&1

对于这种特殊情况,有一个快捷语法:&>。。。或>&

$ ls -ld /tmp /tnt &>/dev/null

$ ls -ld /tmp /tnt >&/dev/null

注意:如果2>&1存在,1>&2也是正确的语法:

$ ls -ld /tmp /tnt 2>/dev/null 1>&2

4b-现在,我将让您思考:

$ ls -ld /tmp /tnt 2>&1 1>&2  | sed -e s/^/++/
++/bin/ls: cannot access /tnt: No such file or directory
++drwxrwxrwt 193 root root 196608 Feb  9 11:08 /tmp/

$ ls -ld /tmp /tnt 1>&2 2>&1  | sed -e s/^/++/
/bin/ls: cannot access /tnt: No such file or directory
drwxrwxrwt 193 root root 196608 Feb  9 11:08 /tmp/

4c-如果您对更多信息感兴趣

您可以点击以下按钮来阅读精细手册:

man -Len -Pless\ +/^REDIRECTION bash

在bash控制台中;-)

注意,1>&2不能与2>&1互换使用。

假设您的命令依赖于管道,例如:docker日志1b3e97c49e39 2>&1|grep“一些日志”grepping将在stderr和stdout之间发生,因为stderr基本上被合并到stdout中。

但是,如果您尝试:docker日志1b3e97c49e39 1>&2|grep“一些日志”,grepping根本不会在任何地方搜索,因为Unix管道通过连接stdout|stdin来连接进程,而在第二种情况下,stdout被重定向到stderr,Unix管道对此没有兴趣。