Bash

“exec &>/dev/null”中間的&做什麼?

  • August 27, 2019

我的命令是:

exec &>/dev/null

這個 & 和 full 命令在這裡做什麼?我知道它正在被重定向到位桶。

&>,不僅僅是&

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.                           

引用自:https://unix.stackexchange.com/questions/374008