What does & mean exactly in output redirection?
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:
Create a file
koko
with the following content:#!bin/bash ls j1 echo "koko2"
Make it executable:
chmod u+x koko
Now note that
j1
doesn't existNow run
./koko &> output
run
cat output
and you will seels: 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.
> FILE 2>&1
and &> FILE
are equivalent. See 8.2.3.2. Redirection of errors of in Bash Guide for Beginners Chapter 8