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


Answers:


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.                           

原始示例的完全非Bash等效项是exec 2>&1 > /dev/null
trr

6
@trr否,这将首先将标准错误重定向到标准输出到达的位置,然后将标准输出重定向到/dev/null(但不是标准错误)。等效于exec >/dev/null 2>&1。重定向的顺序很重要。
库桑兰达

您是对的,我感到困惑
trr

1
@trr不用担心。
库桑兰达
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.