What does & mean exactly in output redirection?
Solution 1:
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.
Solution 2:
> FILE 2>&1
and &> FILE
are equivalent. See 8.2.3.2. Redirection of errors of in Bash Guide for Beginners Chapter 8
Solution 3:
The [n]>&word
is called Duplicating Output File Descriptor(see section 2.7.6 of POSIX Shell Language Standard). This particular behavior is feature of bourne-like shells, including ksh
, dash
, and bash
; in fact, the standard is based around Bourne shell and ksh
. Looking into tcsh and csh manuals, they apparently do not provide capability of duplicating any file descriptor, however from the description of >&
, this behaves as &>
in bash
(that is, redirects errors and normal output to file).
In *nix like systems, including Ubuntu, you often hear that everything is file, or rather a file descriptor. The standard output is constant file descriptor 1 and standard error is file descriptor 2. So, > FILE 2>&1
technically means duplicate file descriptor 2 onto file descriptor 1. In other words of this answer :
The 2>&1 tells the shell to give the command a file descriptor 2 that is a duplicate of descriptor 1. (i.e stderr & stdout point to same fd).
The key here is to note that descriptor 1 has to be set first. Because shell processes redirections in left to right order, the command >FILE 2>&1
tells shell to rewire stdout for command
to go into FILE
first, and only then descriptor 2 can become copy of 1, that is 1 and 2 point to same location - FILE
.
This of course goes beyond the standard error and standard output. As show in this answer, by doing 3&>2
...you duplicate (dup2 ) filedescritor 2 onto filedescriptor 3, possibly closing filedescriptor 3 if it's already open
Example of manipulating file descriptors, among many, would be capturing output of dialog
command into variable
It's also worth noting that &>
is specific to bash
. In zsh
this behaves the same, but according to documentation, "...does not have the same effect as ‘> word 2>&1’ in the presence of multios". In POSIX compliant /bin/sh
, this is would be treated as regular redirection with putting command into background. See also, Is there any sh code that is not syntactically valid bash code?.
See also:
- Greg's Wiki: File Descriptors