2

According to the docs:

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 equiva-
   lent to

          >word 2>&1

However, when I run these commands:

echo abc 2>&1 | tee myfile

I get abc and it's streamed to the file.

But with:

echo abcd 2&>1 | tee myfile

No output, and an empty string is inserted in the file. Aren't >& and &> supposed to be the same?

2
  • 3
    The quoted docs don't include a word before &> or >& (only after) , so where did you get the idea you 2&> is the same as the thing being talked about there?
    – muru
    May 1 at 3:37
  • sorry when you say docs do you mean the man page for bash?
    – Adam
    May 1 at 14:02
7

Summary: In the bash shell, there is no difference between >& and &>, but the redirections in the code that you show are different.

In your first piece of code, you use 2>&1 ("redirect standard error to wherever standard output goes").

You then ask why this is not the same as using 2&>1 if >& and &> are the same.

The bash documentation that you quote is specifically about the redirection operators &>word and >&word. These are the same as the standard redirection >word 2>&1, which is also mentioned in the document. Note that there is no mentioning of file-descriptors, the manual does not write [n]&>word or [n]>&word as for some of the other redirection operators (it is unclear what this would mean as the two redirection operators already involves standard input and standard error implicitly).

Only the second of your two commands, the one using 2&>1, is using one of these redirection operators, and it's not interpreted the way you think. The first command echo abcd 2>&1 is not using the special non-standard redirection >& since it is immediately followed by a number. The manual makes a special mentioning of this:

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.

Your second command, echo abcd 2&>1, is the same as echo abcd 2 &> 1, i.e. "call echo with the two arguments abcd and 2, and write standard output and standard error to the file called 1."

4
  • 1
    Future visitors, especially those from Google, are going to be misled by your opening sentence there is no difference between. Better rephrase it to reduce such potential.
    – iBug
    May 1 at 15:32
  • I'd avoid >&word at all since it'll sooner or later be confused with >&[n] (fd redirection).
    – iBug
    May 1 at 15:34
  • 1
    @iBug Well, there is no difference between the >& and &> redirection operators. This is clearly stated in the manual. If you tag on random words and digits to either side of them, you change them and it's not the same thing any longer, just like the word "hello" becomes different if you add a "c" at the front of it.
    – Kusalananda
    May 1 at 16:05
  • 1
    @iBug Regarding you second comment: This is already a fact made clear in the manual: "Of the two forms, the first is preferred." (followed up with the bit I quote in my answer).
    – Kusalananda
    May 1 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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