Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 3 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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.