&> 在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:55:56  来源:igfitidea点击:

What does &> do in bash?

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 1in 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. 2means stderr.

如果省略文件描述符,则默认为0(stdin) 输入或1(stdout) 输出。 2意味着标准错误。

So we have:

所以我们有:

  • >namemeans 1>name-- redirect stdout to the file name
  • &>nameis like 1>name 2>name-- redirect stdout and stderr to the file name(however nameis only opened once; if you actually wrote 1>name 2>nameit'd try to open nametwice 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 2actually gets passed as an argument to git 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 1with the contents being the result of git status 2-- i.e. the status of the file called 2which 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 >&wordredirects both stdoutand stderrto the result of the expansion of word. In the cases above that is the file 1.

&>word(和>&word重定向都stdoutstderr到字的扩展的结果。在上面所述的情况下是文件1

2>&1redirects stderr(fd 2) to the current value of stdout(fd 1). (Doing this beforeredirecting stdoutlater 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>&1which combines the two fds into one sending to the same location.)

2>&1stderr(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/nullis, in fact, actually running git status 2with a redirection of &>1(stdoutand stderrto 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(stdoutstderr到文件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