0

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 `&'

2 Answers 2

4

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 &>&.

2

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".

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.