&> 在 bash 中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24793069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does &> do in bash?
提问by merlin2011
I was looking at pre-commit hook and discovered the following line because I was wondering why I always got an empy file called 1
in my directory after doing a commit.
我正在查看 pre-commit hook 并发现以下行,因为我想知道为什么1
在执行提交后我总是在我的目录中调用一个 empy 文件。
git status 2&>1 > /dev/null
I believe the intent was to write the following, and I corrected it.
我相信目的是写以下内容,我更正了它。
git status 2>&1 > /dev/null
However, I was curious about what the following syntax does exactly, so I looked up the man page.
但是,我很好奇下面的语法到底做了什么,所以我查阅了手册页。
git status 2&>1
Here is the man page.
这是手册页。
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be redirected to the
file whose name is the expansion of word.
There are two formats for redirecting standard output and standard
error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equiva‐
lent to
>word 2>&1
However, this man page implies that the two are equivalent, which does not seem to be the case.
但是,此手册页暗示两者是等效的,但情况似乎并非如此。
Can someone clarify the man page and explain exactly what is happening with this syntax?
有人可以澄清手册页并准确解释这种语法发生了什么吗?
回答by M.M
The operators we are using here are:
我们在这里使用的运算符是:
>
Syntax: file_descriptoropt>
file_name>&
Syntax: file_descriptoropt>&
file_descriptor&>
Syntax:&>
file_name
>
语法:file_descriptor opt>
file_name>&
语法:file_descriptor opt>&
file_descriptor&>
语法:&>
file_name
If the file descriptor is omitted, the default is 0
(stdin) for input, or 1
(stdout) for output. 2
means stderr.
如果省略文件描述符,则默认为0
(stdin) 输入或1
(stdout) 输出。 2
意味着标准错误。
So we have:
所以我们有:
>name
means1>name
-- redirect stdout to the filename
&>name
is like1>name 2>name
-- redirect stdout and stderr to the filename
(howevername
is only opened once; if you actually wrote1>name 2>name
it'd try to openname
twice and perhaps malfunction).
>name
意思是1>name
——将标准输出重定向到文件name
&>name
就像1>name 2>name
- 将 stdout 和 stderr 重定向到文件name
(但是name
只打开一次;如果你真的写了1>name 2>name
它会尝试打开name
两次,可能会出现故障)。
So when you write git status 2&>1
, it is therefore like git status 2 1>1 2>1
, i.e.
所以当你写的时候git status 2&>1
,它就像git status 2 1>1 2>1
,即
- the first
2
actually gets passed as an argument togit status
. - stdout is redirected to the file named
1
(not the file descriptor 1) - stderr is redirected to the file named
1
- 第一个
2
实际上作为参数传递给git status
. - stdout 被重定向到指定的文件
1
(不是文件描述符 1) - stderr 被重定向到名为的文件
1
This command should actually create a file called 1
with the contents being the result of git status 2
-- i.e. the status of the file called 2
which is probably "Your branch is upto-date, nothing to commit, working directory clean", presuming you do not actually track a file called 2
.
该命令实际上应该创建一个名为的文件1
,其内容是git status 2
- 即被调用的文件的状态,2
它可能是“您的分支是最新的,没有提交,工作目录干净”,假设您实际上没有跟踪一个名为2
.
回答by Etan Reisner
&>word
(and >&word
redirects both stdout
and stderr
to the result of the expansion of word. In the cases above that is the file 1
.
&>word
(和>&word
重定向都stdout
和stderr
到字的扩展的结果。在上面所述的情况下是文件1
。
2>&1
redirects stderr
(fd 2) to the current value of stdout
(fd 1). (Doing this beforeredirecting stdout
later in the line does not do what you might expect and will split the outputs instead of keeping them combined and is a very common shell scripting error. Contrast this to >word 2>&1
which combines the two fds into one sending to the same location.)
2>&1
将stderr
(fd 2)重定向到stdout
(fd 1)的当前值。(在重定向stdout
之后的行中执行此操作不会执行您可能期望的操作,并且会拆分输出而不是将它们组合在一起,这是一个非常常见的 shell 脚本错误。对比这>word 2>&1
将两个 fds 组合成一个发送到同一位置.)
$ { echo stdout; echo stderr >&2; }
stdout
stderr
$ { echo stdout; echo stderr >&2; } >/dev/null
stderr
$ { echo stdout; echo stderr >&2; } >/dev/null 2>&1
$
{ echo stdout; echo stderr >&2; } 2>&1 >/dev/null
stderr
Not that those are, while similar looking, not the same thing.
并不是说那些虽然看起来相似,但不是一回事。
git status 2&>1 > /dev/null
is, in fact, actually running git status 2
with a redirection of &>1
(stdout
and stderr
to file 1
). Almost certainly not what was intended. Your correction almost certainly is what was intended.
git status 2&>1 > /dev/null
实际上,实际上是在git status 2
重定向&>1
(stdout
和stderr
到文件1
) 的情况下运行的。几乎可以肯定不是预期的。您的更正几乎可以肯定是有意的。
$ git init repro
Initialized empty Git repository in /tmp/repro/.git/
$ cd repro/
$ git status
# On branch master
#
# Initial commit
#
nothing to commit
$ ls
$ git status 2>&1
# On branch master
#
# Initial commit
#
nothing to commit
$ ls
$ git status 2&>1
$ ls
1
$ cat 1
# On branch master
#
# Initial commit
#
nothing to commit