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?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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.
&> says send both,
standard output and
standard error, somewhere. For instance,
ls <non-existent_file> &> out.file. Let me illustrate this with an example.
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 exist
./koko &> output
cat output and you will see
ls: cannot access 'j1': No such file or directory koko2
standard error (
ls: cannot access 'j1': No such file or directory) and
standard output (
koko2), were sent to the file
Now run it again but this time like so:
./koko > output
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.
[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
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
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 -
This of course goes beyond the standard error and standard output. As show in this answer, by doing
...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
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?.