Ubuntu – What does & mean exactly in output redirection

command lineioredirect

I see stuff like command 1> out or with 2>&1 to redirect stderr, but sometimes I also see &> by itself, etc.

What is the best way to understand & and what it means exactly?

Best Answer

  • The & in 2>&1 simply says that the number 1 is a file descriptor and not a file name. In this case the standard output file descriptor.

    If you use 2>1, then this would redirect errors to a file called 1 but if you use 2>&1, then it would send it to the standard output stream.

    This &> says send both, standard output and standard error, somewhere. For instance, ls <non-existent_file> &> out.file. Let me illustrate this with an example.

    Setup:

    1. Create a file koko with the following content:

      #!bin/bash
      
      ls j1
      echo "koko2"
      
    2. Make it executable: chmod u+x koko

    3. Now note that j1 doesn't exist

    4. Now run ./koko &> output

    5. run cat output and you will see

      ls: cannot access 'j1': No such file or directory
      koko2
      

    Both, standard error (ls: cannot access 'j1': No such file or directory) and standard output (koko2), were sent to the file output.

    Now run it again but this time like so:

    ./koko > output
    

    Do cat output and you will only see the koko2 like. But not the error output from the ls j1 command. That will be sent to the standard error which you will see in your terminal.

    Important note thanks to @Byte Commander:

    Note that in command >file 2>&1 the order of the redirection is important. If you write command 2>&1 >file instead (which is normally not what you want), it will first redirect the command's stdout to the file and after that redirect the command's stderr to its now unused stdout, so it will show up in the terminal and you could pipe it or redirect it again, but it will not be written to the file.