& 在“exec &>/dev/null”中间做什么?

Wil*_*oss 11 shell bash io-redirection exec

我的命令是:

exec &>/dev/null
Run Code Online (Sandbox Code Playgroud)

这个 & 和完整命令在这里做什么?我知道它被重定向到位桶。

Kus*_*nda 22

&>,不仅仅是&

在 中bash&>将标准输出流和标准错误流都重定向到某处。

因此,utility &>/dev/null与 相同utility >/dev/null 2>&1

该命令exec &>/dev/null将当前 shell 的两个输出流重定向到/dev/null(即它丢弃从该点开始的所有脚本输出,错误或其他)。

bash手册的相关部分:

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         
   equivalent to                                                           

          >word 2>&1                                                       

   When using the second form, word may not expand to a number or -.  If   
   it does, other redirection operators apply (see Duplicating File        
   Descriptors below) for compatibility reasons.                           
Run Code Online (Sandbox Code Playgroud)

  • @trr 不,这将首先将标准错误重定向到标准输出所在的位置,然后 _then_ 将标准输出重定向到 `/dev/null`(但不是标准错误)。它等价于`exec >/dev/null 2>&1`。重定向的顺序很重要。 (6认同)