&>是什么做bash吗? [英] What does &> do in bash?

查看:74
本文介绍了&>是什么做bash吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看预提交钩子,并发现了以下行,因为我想知道为什么在执行提交后总是在目录中得到一个名为1的empy文件.

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.

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

这是手册页.

  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?

推荐答案

我们在这里使用的运算符是:

The operators we are using here are:

  • >语法: file_descriptor opt > file_name
  • >&语法: file_descriptor opt >& file_descriptor
  • &>语法:&> 文件名
  • > Syntax: file_descriptoropt > file_name
  • >& Syntax: file_descriptoropt >& file_descriptor
  • &> Syntax: &> file_name

如果省略文件描述符,则默认输入为0(stdin),输出为1(stdout). 2表示标准错误.

If the file descriptor is omitted, the default is 0 (stdin) for input, or 1 (stdout) for output. 2 means stderr.

所以我们有:

  • >name表示1>name-将stdout重定向到文件name
  • &>name就像1>name 2>name-将stdout和stderr重定向到文件name(但是name仅被打开一次;如果您实际编写了1>name 2>name,它将尝试两次打开name可能还有故障).
  • >name means 1>name -- redirect stdout to the file name
  • &>name is like 1>name 2>name -- redirect stdout and stderr to the file name (however name is only opened once; if you actually wrote 1>name 2>name it'd try to open name twice and perhaps malfunction).

因此,当您编写git status 2&>1时,它就像git status 2 1>1 2>1一样,即

So when you write git status 2&>1, it is therefore like git status 2 1>1 2>1 , i.e.

  • 第一个2实际上是作为参数传递给git status的.
  • stdout重定向到名为1的文件(不是文件描述符1)
  • stderr重定向到名为1
  • 的文件
  • the first 2 actually 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

此命令实际上应创建一个名为1的文件,其内容为git status 2的结果-即名为2的文件的状态,其状态可能是您的分支是最新的,无需提交任何内容,工作目录干净",假设您实际上并未跟踪名为2的文件.

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.

这篇关于&>是什么做bash吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆