要将stderr和stdout合并到stdout流中,我们将其附加到命令中:
2>&1
例如,查看编译g++main.cpp时的前几个错误:
g++ main.cpp 2>&1 | head
2>&1的详细含义是什么?
要将stderr和stdout合并到stdout流中,我们将其附加到命令中:
2>&1
例如,查看编译g++main.cpp时的前几个错误:
g++ main.cpp 2>&1 | head
2>&1的详细含义是什么?
当前回答
要将stdout重定向到file.txt:
echo test > file.txt
这相当于:
echo test 1> file.txt
要将stderr重定向到file.txt:
echo test 2> file.txt
所以>是将流重定向到另一个文件描述符的语法(&I):
0是标准输入1是标准输出2是标准错误
要将stdout重定向到stderr:
echo test 1>&2 # equivalently, echo test >&2
要将stderr重定向到stdout:
echo test 2>&1
因此,在2>&1中:
2> 将stderr重定向到(未指定)文件。&1将stderr重定向到stdout。
其他回答
如果系统上不存在/foo,而/tmp确实存在…
$ ls -l /tmp /foo
将打印/tmp的内容并为/foo打印错误消息
$ ls -l /tmp /foo > /dev/null
将/tmp的内容发送到/dev/null并为/foo打印错误消息
$ ls -l /tmp /foo 1> /dev/null
将完全相同(注意1)
$ ls -l /tmp /foo 2> /dev/null
将打印/tmp的内容并将错误消息发送到/dev/null
$ ls -l /tmp /foo 1> /dev/null 2> /dev/null
将向/dev/null发送列表和错误消息
$ ls -l /tmp /foo > /dev/null 2> &1
是速记
2是控制台标准错误。
1是控制台标准输出。
这是标准的Unix,Windows也遵循POSIX。
例如,当您跑步时
perl test.pl 2>&1
标准错误被重定向到标准输出,因此您可以同时看到两个输出:
perl test.pl > debug.log 2>&1
执行后,您可以在debug.log中看到所有输出,包括错误。
perl test.pl 1>out.log 2>err.log
然后标准输出转到out.log,标准错误转到err.log。
我建议你试着理解这些。
文件描述符1是标准输出(stdout)。文件描述符2是标准错误(stderr)。
首先,2>1可能是将stderr重定向到stdout的好方法。然而,它实际上会被解释为“将stderr重定向到名为1的文件”。
&指示后面和前面的是文件描述符,而不是文件名。因此,我们使用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。
这允许一些相当复杂的诡计。
从程序员的角度来看,这意味着:
dup2(1, 2);
请参见手册页。
了解2>&1是副本也解释了为什么。。。
command >file 2>&1
…与…不同。。。
command 2>&1 >file
第一个将两个流发送到文件,而第二个将错误发送到stdout,并将普通输出发送到文件。