What does &> do in bash?
Solution 1:
The operators we are using here are:
-
>
Syntax: file_descriptoropt>
file_name -
>&
Syntax: file_descriptoropt>&
file_descriptor -
&>
Syntax:&>
file_name
If the file descriptor is omitted, the default is 0
(stdin) for input, or 1
(stdout) for output. 2
means stderr.
So we have:
-
>name
means1>name
-- redirect stdout to the filename
-
&>name
is like1>name 2>name
-- redirect stdout and stderr to the filename
(howevername
is only opened once; if you actually wrote1>name 2>name
it'd try to openname
twice and perhaps malfunction).
So when you write git status 2&>1
, it is therefore like git status 2 1>1 2>1
, i.e.
- the first
2
actually gets passed as an argument togit status
. - stdout is redirected to the file named
1
(not the file descriptor 1) - stderr is redirected to the file named
1
This command should actually create a file called 1
with the contents being the result of git status 2
-- i.e. the status of the file called 2
which is probably "Your branch is upto-date, nothing to commit, working directory clean", presuming you do not actually track a file called 2
.
Solution 2:
&>word
(and >&word
redirects both stdout
and stderr
to the result of the expansion of word. In the cases above that is the file 1
.
2>&1
redirects stderr
(fd 2) to the current value of stdout
(fd 1). (Doing this before redirecting stdout
later in the line does not do what you might expect and will split the outputs instead of keeping them combined and is a very common shell scripting error. Contrast this to >word 2>&1
which combines the two fds into one sending to the same location.)
$ { echo stdout; echo stderr >&2; }
stdout
stderr
$ { echo stdout; echo stderr >&2; } >/dev/null
stderr
$ { echo stdout; echo stderr >&2; } >/dev/null 2>&1
$
{ echo stdout; echo stderr >&2; } 2>&1 >/dev/null
stderr
Not that those are, while similar looking, not the same thing.
git status 2&>1 > /dev/null
is, in fact, actually running git status 2
with a redirection of &>1
(stdout
and stderr
to file 1
). Almost certainly not what was intended. Your correction almost certainly is what was intended.
$ git init repro
Initialized empty Git repository in /tmp/repro/.git/
$ cd repro/
$ git status
# On branch master
#
# Initial commit
#
nothing to commit
$ ls
$ git status 2>&1
# On branch master
#
# Initial commit
#
nothing to commit
$ ls
$ git status 2&>1
$ ls
1
$ cat 1
# On branch master
#
# Initial commit
#
nothing to commit