有什么作用” 2>& 1"意思是?
要将 stderr
和 stdout
组合到 stdout
流中,我们将其附加到一个命令:
2>&1
例如,以下命令显示了前几个错误编译 main.cpp
:
g++ main.cpp 2>&1 | head
但是 2> 1
是什么意思?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
文件描述符1是标准输出(
stdout
)。文件描述符2是标准错误(
stderr
)。首先,
2> 1
看起来像是将重定向stderr
重定向到stdout
的好方法。但是,它实际上将被解释为“重定向stderr
到名为1
”的文件。&
指示下面的内容是文件描述符,而不是文件名。因此,我们使用2>& 1
。考虑>&
成为重定向合并操作员。File descriptor 1 is the standard output (
stdout
).File descriptor 2 is the standard error (
stderr
).At first,
2>1
may look like a good way to redirectstderr
tostdout
. However, it will actually be interpreted as "redirectstderr
to a file named1
".&
indicates that what follows and precedes is a file descriptor, and not a filename. Thus, we use2>&1
. Consider>&
to be a redirect merger operator.将 stdout 重定向到
file.txt
:这相当于:
将 stderr 重定向到
file.txt
:所以
>&
是语法将重定向流到另一个文件描述符:将stdout 重定向到stderr:
将stderr 重定向到stdout :
因此,在
2>&1
:2>
将 stderr 重定向到一个(未指定的)文件。&1
将 stderr 重定向到 stdout。To redirect stdout to
file.txt
:This is equivalent to:
To redirect stderr to
file.txt
:So
>&
is the syntax to redirect a stream to another file descriptor:To redirect stdout to stderr:
To redirect stderr to stdout:
Thus, in
2>&1
:2>
redirects stderr to an (unspecified) file.&1
redirects stderr to stdout.关于重定向的一些技巧,
一些关于此的语法特殊性可能具有重要的行为。关于重定向,
stderr
,stdout
和参数订购有一些小样本。1-覆盖还是附加?
符号
>
表示重定向。>
表示发送到整个完整的文件,如果存在,则覆盖目标(请参阅noclobber
bash功能#3 稍后)。>>
表示以外的以外发送将附加到目标,如果存在。无论如何,如果文件不存在,则将创建文件。
2- shell命令行是订单依赖!
为了测试此问题,我们需要一个简单的命令,它将在两个输出上发送某些内容 :(
期望您没有名为
/tnt
的目录,当然;)。好吧,我们有!因此,让我们看看:
最后一个命令行
stderr
到控制台,这似乎不是预期的行为...但是...如果您想进行一些 post Filtering < /em>关于 standard 输出, error 输出或两者:
请注意,本段中的最后一个命令行与上一段完全相同,我写了不是预期的行为(因此,这甚至可能是预期的行为)。
好吧,关于重定向的一些技巧,因为在两个输出上进行不同的操作:
注意::
&amp; 9
描述符,因为)9&gt;&amp; 2
。附录:nota!带有新版本的 bash (
&gt; 4.0
)有一个新功能和更性感的语法用于执行此类操作:最后,对于这种级联输出格式:
附录:nota! 在这两种方面相同的新语法:
其中
stdout
通过特定的过滤器,stderr
到另一个,最后两个输出都通过第三个命令过滤器进行。2b-使用
|&amp;
而不是语法
命令|&amp; ...
可以用作 alias命令2&gt;&amp; 1 | ...
。有关命令行顺序的相同规则也适用。 操作员的含义|&amp;什么是什么意思,在bash中?3-关于
noclobber
选项和&gt; |
语法>compriting :
while
set set- o noclobber
指示bash to 不是覆盖任何现有文件,&gt; |
语法让您通过此限制:每次文件都被覆盖,现在好吧:
通过
&gt; |
通过:解开此选项和/或查询是否已经设置。
4-最后的技巧等等...
为了重定向 从给定命令输出,我们看到正确的语法可能是:
对于此 special 案例,有一个快捷方式语法:
&amp;&gt;
...或&gt;&amp;
nota:如果
2&gt;&gt; 1
存在,1&gt;&amp; 2
也是正确的语法:4B-现在,我会让您考虑:
4C-如果您对 更多信息
您可以通过命中来读取精细手册:
在A bash 控制台;-)
Some tricks about redirection
Some syntax particularity about this may have important behaviours. There is some little samples about redirections,
STDERR
,STDOUT
, and arguments ordering.1 - Overwriting or appending?
Symbol
>
means redirection.>
means send to as a whole completed file, overwriting target if exist (seenoclobber
bash feature at #3 later).>>
means send in addition to would append to target if exist.In any case, the file would be created if they not exist.
2 - The shell command line is order dependent!!
For testing this, we need a simple command which will send something on both outputs:
(Expecting you don't have a directory named
/tnt
, of course ;). Well, we have it!!So, let's see:
The last command line dumps
STDERR
to the console, and it seem not to be the expected behaviour... But...If you want to make some post filtering about standard output, error output or both:
Notice that the last command line in this paragraph is exactly same as in previous paragraph, where I wrote seem not to be the expected behaviour (so, this could even be an expected behaviour).
Well, there is a little tricks about redirections, for doing different operation on both outputs:
Note:
&9
descriptor would occur spontaneously because of) 9>&2
.Addendum: nota! With the new version of bash (
>4.0
) there is a new feature and more sexy syntax for doing this kind of things:And finally for such a cascading output formatting:
Addendum: nota! Same new syntax, in both ways:
Where
STDOUT
go through a specific filter,STDERR
to another and finally both outputs merged go through a third command filter.2b - Using
|&
insteadSyntax
command |& ...
could be used as an alias forcommand 2>&1 | ...
. Same rules about command line order applies. More details at What is the meaning of operator |& in bash?3 - A word about
noclobber
option and>|
syntaxThat's about overwriting:
While
set -o noclobber
instruct bash to not overwrite any existing file, the>|
syntax let you pass through this limitation:The file is overwritten each time, well now:
Pass through with
>|
:Unsetting this option and/or inquiring if already set.
4 - Last trick and more...
For redirecting both output from a given command, we see that a right syntax could be:
for this special case, there is a shortcut syntax:
&>
... or>&
Nota: if
2>&1
exist,1>&2
is a correct syntax too:4b- Now, I will let you think about:
4c- If you're interested in more information
You could read the fine manual by hitting:
in a bash console ;-)
我发现了有关重定向的出色帖子:
将标准输出和标准误差重定向到文件
此单线使用
&amp;&gt;
运算符将两个输出流 - stdout和stderr-从命令转换为文件。这是Bash的快捷方式,可以快速将两个流都重定向到同一目的地。这是文件描述符表在BASH重定向两个流之后的样子:
如您所见,Stdout和stderr现在都指向
file
。因此,写给Stdout和Stderr的任何内容都将写入文件
。有几种方法可以将这两个流都重定向到同一目的地。您可以接一个地重定向每个流:
这是将两个流将两个流都重定向到文件的更常见的方法。首先将STDOUT重定向到文件,然后将STDERR复制为与Stdout相同。因此,两个流都指向
文件
。当Bash看到几个重定向时,它将它们从左到右处理。让我们浏览这些步骤,看看如何发生。在运行任何命令之前,BASH的文件描述符表看起来像:
我们以前已经看过这一点,它使stdout点要归档:
Next Bash看到第二个重定向2&gt;&amp; 1。我们以前从未见过这个重定向。这个重复文件描述符2是文件描述符1的副本,我们得到:
两个流都被重定向到文件。
但是,请小心在这里!写作
与写作不同:
重定向的顺序提交了bash的重要性!此命令仅将标准输出重定向到文件。 Stderr仍将打印到终端。要了解为什么会发生这种情况,让我们再次浏览步骤。因此,在运行命令之前,文件描述符表看起来像这样:
现在击败重定向,从左到右。它首先看到2&gt;&amp; 1,因此将stderr复制到Stdout。文件描述符表变为:
现在bash看到第二个重定向,
&gt; file
,然后将其重定向到文件:这里?现在,Stdout指向文件,但Stderr仍然指向终端!写信给stderr的所有内容仍然将其打印到屏幕上!因此,重定向的顺序要非常非常小心!
另请注意,在狂欢中,写作
与:
I found this brilliant post on redirection: All about redirections
Redirect both standard output and standard error to a file
This one-liner uses the
&>
operator to redirect both output streams - stdout and stderr - from command to file. This is Bash's shortcut for quickly redirecting both streams to the same destination.Here is how the file descriptor table looks like after Bash has redirected both streams:
As you can see, both stdout and stderr now point to
file
. So anything written to stdout and stderr gets written tofile
.There are several ways to redirect both streams to the same destination. You can redirect each stream one after another:
This is a much more common way to redirect both streams to a file. First stdout is redirected to file, and then stderr is duplicated to be the same as stdout. So both streams end up pointing to
file
.When Bash sees several redirections it processes them from left to right. Let's go through the steps and see how that happens. Before running any commands, Bash's file descriptor table looks like this:
Now Bash processes the first redirection >file. We've seen this before and it makes stdout point to file:
Next Bash sees the second redirection 2>&1. We haven't seen this redirection before. This one duplicates file descriptor 2 to be a copy of file descriptor 1 and we get:
Both streams have been redirected to file.
However be careful here! Writing
is not the same as writing:
The order of redirects matters in Bash! This command redirects only the standard output to the file. The stderr will still print to the terminal. To understand why that happens, let's go through the steps again. So before running the command, the file descriptor table looks like this:
Now Bash processes redirections left to right. It first sees 2>&1 so it duplicates stderr to stdout. The file descriptor table becomes:
Now Bash sees the second redirect,
>file
, and it redirects stdout to file:Do you see what happens here? Stdout now points to file, but the stderr still points to the terminal! Everything that gets written to stderr still gets printed out to the screen! So be very, very careful with the order of redirects!
Also note that in Bash, writing
is exactly the same as:
这些数字指的是文件描述符 (fd)。
stdin
stdout
stderr
2>&1
将 fd 2 重定向到 1。这有效对于任意数量的文件描述符(如果程序使用它们)。
如果您忘记了,您可以查看
/usr/include/unistd.h
:也就是说,我已经编写了使用非标准文件描述符进行自定义日志记录的 C 工具,因此您看不到它,除非您将其重定向到文件或其他内容。
The numbers refer to the file descriptors (fd).
stdin
stdout
stderr
2>&1
redirects fd 2 to 1.This works for any number of file descriptors if the program uses them.
You can look at
/usr/include/unistd.h
if you forget them:That said I have written C tools that use non-standard file descriptors for custom logging so you don't see it unless you redirect it to a file or something.
我发现这很有帮助
如果您是初学者阅读此更新, 。
在Linux或Unix系统中,有两个地方程序将输出发送到:标准输出(STDOUT)和标准错误(stderr)。您可以将这些输出重定向到任何文件。
就像您这样做
ls -a&gt; output.txt
在控制台中什么都不会打印到所有输出(stdout)被重定向到输出文件。
,如果您尝试打印任何不退出的文件的内容,则意味着输出将是一个错误,例如如果您当前目录中不存在的打印test.txt
cat test.txt&gt; error.txt
输出将是
错误。TXT文件将是空的,因为我们将Stdout重定向到文件而非STDERR。
因此,我们需要文件描述符(文件描述符不过是一个代表打开文件的正整数。您可以说描述符是文件的唯一ID)来告诉Shell我们发送到哪种类型的输出 1用于Stdout,2用于STDERR 。
因此,如果您执行此操作
ls -a 1&gt; output.txt
表示您将标准输出(STDOUT)发送到output.txt。如果您这样做
&amp; 1
用于引用文件描述符1(STDOUT)的值。现在到点
2&gt;&amp; 1
“将stderr重定向到我们正在重定向的同一位置”现在,您可以执行此操作
&lt; br
cat也许file.txt&gt; output.txt 2&gt;&amp; 1
标准输出(STDOUT)和标准误差(STDERR)都将重定向到output.txt。
感谢 ondrej K.
I found this very helpful if you are a beginner read this
Update:
In Linux or Unix System there are two places programs send output to: Standard output (stdout) and Standard Error (stderr).You can redirect these output to any file.
Like if you do this
ls -a > output.txt
Nothing will be printed in console all output (stdout) is redirected to output file.
And if you try print the content of any file that does not exits means output will be an error like if you print test.txt that not present in current directory
cat test.txt > error.txt
Output will be
But error.txt file will be empty because we redirecting the stdout to a file not stderr.
so we need file descriptor( A file descriptor is nothing more than a positive integer that represents an open file. You can say descriptor is unique id of file) to tell shell which type of output we are sending to file .In Unix /Linux system 1 is for stdout and 2 for stderr.
so now if you do this
ls -a 1> output.txt
means you are sending Standard output (stdout) to output.txt.and if you do this
cat test.txt 2> error.txt
means you are sending Standard Error (stderr) to error.txt .&1
is used to reference the value of the file descriptor 1 (stdout).Now to the point
2>&1
means “Redirect the stderr to the same place we are redirecting the stdout”Now you can do this
<br
cat maybefile.txt > output.txt 2>&1
both Standard output (stdout) and Standard Error (stderr) will redirected to output.txt.
Thanks to Ondrej K. for pointing out
该构造将标准错误流(
stderr
)发送到当前标准输出的位置(stdout
) - 此货币问题似乎已被忽略通过其他答案。您可以通过使用此方法将任何输出句柄重定向到他人,但最常用于引导
stdout
和stderr
流中的单个流进行处理。一些示例是:
请注意,最后一个将不是 direct
stderr
outfile2
- 它将其重定向到stdout
>是当遇到参数(OUTFILE1
)和然后 redirectsstdout
offile2
时。这样可以实现一些非常复杂的骗局。
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
andstderr
streams into a single stream for processing.Some examples are:
Note that that last one will not direct
stderr
tooutfile2
- it redirects it to whatstdout
was when the argument was encountered (outfile1
) and then redirectsstdout
tooutfile2
.This allows some pretty sophisticated trickery.
2是控制台标准误差。
1是控制台标准输出。
这是标准UNIX,Windows也遵循POSIX。
例如,当您运行
标准错误时,将重定向到标准输出,因此您可以一起看到两个输出:
执行后,您可以在debug.log中查看所有输出,包括错误。
然后,标准输出将输出。log,以及err.log的标准错误。
我建议您尝试理解这些。
2 is the console standard error.
1 is the console standard output.
This is the standard Unix, and Windows also follows the POSIX.
E.g. when you run
the standard error is redirected to standard output, so you can see both outputs together:
After execution, you can see all the output, including errors, in the debug.log.
Then standard output goes to out.log, and standard error to err.log.
I suggest you to try to understand these.
2>&1
是 POSIX shell 结构。以下是逐个标记的细分:2
:“标准错误”输出文件描述符。>&
: 复制输出文件描述符 运算符(输出重定向运算符> )。给定
[x]>&[y]
,由x
表示的文件描述符将成为输出文件描述符y
的副本>。1
“标准输出”输出文件描述符。表达式
2>&1
将文件描述符1
复制到位置2
,因此任何输出都会写入2
(执行环境中的“标准错误”)会转到最初由1
描述的同一文件(“标准输出”)。进一步说明:
文件描述符: “每个进程唯一的非负整数,用于识别打开的文件以进行文件访问。”
标准输出/错误:请参阅
2>&1
is a POSIX shell construct. Here is a breakdown, token by token:2
: "Standard error" output file descriptor.>&
: Duplicate an Output File Descriptor operator (a variant of Output Redirection operator>
). Given[x]>&[y]
, the file descriptor denoted byx
is made to be a copy of the output file descriptory
.1
"Standard output" output file descriptor.The expression
2>&1
copies file descriptor1
to location2
, so any output written to2
("standard error") in the execution environment goes to the same file originally described by1
("standard output").Further explanation:
File Descriptor: "A per-process unique, non-negative integer used to identify an open file for the purpose of file access."
Standard output/error: Refer to the following note in the Redirection section of the shell documentation:
要回答您的问题:它需要任何错误输出(通常发送到STDERR)并将其写入标准输出(Stdout)。
这很有帮助,例如,当您需要所有输出的分页时,“更多”。一些程序喜欢将用法信息打印到stderr中。
为了帮助您记住
“ 2&gt;&amp; 1”只是指向发送给STDERR的所有内容,而是指向STDOUT。
我还建议阅读此帖子重定向涵盖此主题详细详细。
To answer your question: It takes any error output (normally sent to stderr) and writes it to standard output (stdout).
This is helpful with, for example 'more' when you need paging for all output. Some programs like printing usage information into stderr.
To help you remember
"2>&1" simply points everything sent to stderr, to stdout instead.
I also recommend reading this post on error redirecting where this subject is covered in full detail.
从程序员的角度来看,这恰恰意味着:
请参阅手册页。
了解
2>&1
是一个副本也解释了为什么 ...... 与 ... 不同。
第一个会将两个流发送到
file
,而第二个会将错误发送到stdout
,并将普通输出发送到file
。From a programmer's point of view, it means precisely this:
See the man page.
Understanding that
2>&1
is a copy also explains why ...... is not the same as ...
The first will send both streams to
file
, whereas the second will send errors tostdout
, and ordinary output intofile
.大家,永远记住paxdiablo关于重定向目标当前位置的提示...它很很重要。
我对
2>&1
运算符的个人记忆法是这样的:&
视为'and'
或'add '
(该字符是一个amps-和,不是吗?)2
(stderr) 到哪里1
(标准输出)已经/当前是并且添加两个流'。同样的助记符也适用于其他常用的重定向,
1>&2
:&
视为and
或add
...(您明白了与号的概念,是吗?)1
(stdout) 重定向到2
( stderr)已经/当前是并且添加两个流'。永远记住:您必须“从末尾”、从右到左(而不是从左到右)阅读重定向链。
People, always remember paxdiablo's hint about the current location of the redirection target... It is important.
My personal mnemonic for the
2>&1
operator is this:&
as meaning'and'
or'add'
(the character is an ampers-and, isn't it?)2
(stderr) to where1
(stdout) already/currently is and add both streams'.The same mnemonic works for the other frequently used redirection too,
1>&2
:&
meaningand
oradd
... (you get the idea about the ampersand, yes?)1
(stdout) to where2
(stderr) already/currently is and add both streams'.And always remember: you have to read chains of redirections 'from the end', from right to left (not from left to right).
参考:
键入
/^redirect
定位到重定向
部分,并了解更多...在线版本在这里: 3.6 redirections
“ rel =
/www.gnu.org/software/bash/manual/bashref.html#redirections 是学习Linux的强大工具。
Ref:
Type
/^REDIRECT
to locate to theredirection
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.unix_commands 2>&1
这用于将错误打印到终端。
&2
处的“标准错误”缓冲区,以及来自该地址的2
引用和流缓冲。&1
处的“标准输出”缓冲区,以及来自该地址的1
引用和流缓冲。那么回到命令。每当程序
unix_commands
产生错误时,它就会将其写入错误缓冲区。因此,我们创建一个指向该缓冲区2
的指针,并将>
错误重定向到输出缓冲区&1
>。至此我们就完成了,因为输出缓冲区中的任何内容都由终端读取并打印。unix_commands 2>&1
This is used to print errors to the terminal.
&2
, and2
references and streams from that buffer.&1
, and1
references and streams from that buffer.So going back to the command. Anytime the program
unix_commands
produces an error, it writes that into the errors buffer. So we create a pointer to that buffer2
, and redirect>
the errors into the outputs buffer&1
. At this point we're done, because anything in the outputs buffer is read and printed by the terminal.前提是系统上不存在
/foo
代码>/foo
将将
/tmp
的内容发送到/dev/dev/null
,并打印/foo
的错误消息将执行完全相同(注意 1 )
将打印
/tmp
的内容,然后将错误消息发送到/dev/dev/null
将两者都发送列表以及到
/dev/null
的错误消息是速记
Provided that
/foo
does not exist on your system and/tmp
does…will print the contents of
/tmp
and print an error message for/foo
will send the contents of
/tmp
to/dev/null
and print an error message for/foo
will do exactly the same (note the 1)
will print the contents of
/tmp
and send the error message to/dev/null
will send both the listing as well as the error message to
/dev/null
is shorthand
这就像将错误传递到Stdout或终端一样。
也就是说,
cmd
不是命令:错误发送到这样的文件:
标准错误已发送到终端。
This is just like passing the error to the stdout or the terminal.
That is,
cmd
is not a command:The error is sent to the file like this:
Standard error is sent to the terminal.
0 表示输入,1 表示标准输出,2 表示标准错误。
一个提示:
somecmd >1.txt 2>&1
是正确的,而somecmd 2>&1 >1.txt
完全错误没有效果!0 for input, 1 for stdout and 2 for stderr.
One Tip:
somecmd >1.txt 2>&1
is correct, whilesomecmd 2>&1 >1.txt
is totally wrong with no effect!您需要从管道的角度来理解这一点。
正如您所看到的,管道左侧的 stdout 和 stderr 都被输入到(管道的)右侧。
这与
You need to understand this in terms of pipe.
As you can see both stdout and stderr of LHS of pipe is fed into the RHS (of pipe).
This is the same as
请注意,
1>&2
不能与2>&1
互换使用。想象一下您的命令取决于管道,例如:
docker 日志 1b3e97c49e39 2>&1 | grep“一些日志”
grep 将会在
stderr
和stdout
上发生,因为stderr
基本上已合并到stdout
中。但是,如果您尝试:
docker 日志 1b3e97c49e39 1>&2 | grep "一些日志",
grepping 根本不会真正搜索任何地方,因为 Unix 管道通过连接
stdout | 连接进程。第二种情况下的 stdin
和stdout
被重定向到 Unix 管道不感兴趣的stderr
。Note that
1>&2
cannot be used interchangeably with2>&1
.Imagine your command depends on piping, for example:
docker logs 1b3e97c49e39 2>&1 | grep "some log"
grepping will happen across both
stderr
andstdout
sincestderr
is basically merged intostdout
.However, if you try:
docker logs 1b3e97c49e39 1>&2 | grep "some log"
,grepping will not really search anywhere at all because Unix pipe is connecting processes via connecting
stdout | stdin
, andstdout
in the second case was redirected tostderr
in which Unix pipe has no interest.