12

My command is:

exec &>/dev/null

What does this & and full command do here? I know it is being redirected to the bit-bucket.

23

It's &>, not just &.

In bash, &> redirects both the standard output stream and the standard error stream somewhere.

Hence, utility &>/dev/null is the same as utility >/dev/null 2>&1.

The command exec &>/dev/null redirects both output streams of the current shell to /dev/null (i.e. it discards all output of the script from that point on, error or otherwise).

The relevant part of the bash manual:

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.                           
  • The complete non-Bash equivalent of the original example would be exec 2>&1 > /dev/null – trr Jun 29 '17 at 6:31
  • 6
    @trr No, that would first redirect standard error to wherever standard output went, and then redirect standard output to /dev/null (but not standard error). What it's equivalent to is exec >/dev/null 2>&1. The order of the redirections is important. – Kusalananda Jun 29 '17 at 6:34
  • You're right, I was confused – trr Jun 29 '17 at 12:47
  • 1
    @trr No worries. – Kusalananda Jun 29 '17 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.