Sign up ×

Given the preamble, foobar function and invocations of it:

exec 3>/dev/null    
function foobar { echo foo; echo bar >&2; }
foobar >/dev/null
foobar 2>/dev/null
foobar &>/dev/null
foobar &>&3

Why doesn't the last simple command get executed by Bash? Instead Bash quits with:

-bash: syntax error near unexpected token `&'
share|improve this question

2 Answers 2

up vote 4 down vote accepted

That's because there is no operator &>&.

There is an operator &> word that redirects both stdout and stderr to word.

There is an operator [n]>& word that duplicates fd n (default 1) from fd word. As a special case, if n is empty, and word isn't a number, redirects both stdout and stderr to word.

But that's it. There isn't a special syntax for file descriptors which you can combine with operators. There are just the operators, which can interpret their operands as file descriptors or filenames. And there isn't a &>& operator which redirects both stdout and stderr, and interprets its right operand as a file descriptor.

Summary: There is no special syntax &n for file descriptors. The & belongs to the operator, not to the operand. There is no operator &>&.

share|improve this answer

And why would it? "... &>file" is just a syntactic sugar for "... >file 2>&1". Nothing more. If you want to make descriptor 2 point where 3 is pointing (/dev/null), you need to use "... 2>&3".

share|improve this answer

Your Answer


