3

The bash manpage says:

Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

There are two formats for redirecting standard output and standard error:

&>word

and

>&word

Of the two forms, the first is preferred. This is semantically equivalent to

>word 2>&1

Why is the first form preferred?

1
  • I want an explanation of the bash semantics, not the noun's ;-) Commented Oct 29, 2015 at 19:04

1 Answer 1

3

The author is saying that &>word and >&word are both the same, and both equivalent to >word 2>&1. In addition, the first form is preferred.

The immediately following text in the manual says why:

When using the second form, word may not expand to a number or ‘-’. If it does, other redirection operators apply (see Duplicating File Descriptors below) for compatibility reasons.

5
  • "no reason is given for it". This is what I want to know. :-) Commented Oct 29, 2015 at 18:58
  • 2
    Indeed. Because [n]>&word is the syntax for fd duplication when word is a number or - and so using >&word where word is a file is a confusing syntactic collision. I think the better question is why is this even allowed. Commented Oct 29, 2015 at 19:08
  • 2
    Additionally the only reason the the >&file syntax even works is even legal, as far as I can see, is because the spec says "If word evaluates to something else, the behavior is unspecified." Commented Oct 29, 2015 at 19:09
  • 1
    I'm still learning a lot of these strange things, but it seems like one could write a book on "Why is this even allowed in Bash."
    – miken32
    Commented Oct 29, 2015 at 19:10
  • Totally makes sense but I couldn't figure it. That explanation is missing from bash 3.2.25(1)-release (x86_64-redhat-linux-gnu). Commented Oct 29, 2015 at 19:10

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