82

I have seen echo being used like this in many places:

echo >&2 message text ...

What does this mean?

I understand 2>&1, however, I am not sure how to interpret the usage above.

Can anyone please explain?

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 19
    The previse position of the redirection in the command line is not important. All of `>&2 echo message` and `echo >&2 message` and `echo message >&2` are equivalent. – tripleee May 06 '14 at 08:59
  • 2
    Don't forget to accept an answer if you found the explanation you were looking for. – Jite May 07 '15 at 14:26
  • 3
    As an extension to what @tripleee is talking about, do note that `echo hey >/dev/null 2>&1` and `echo hey 2>&1 >/dev/null` is **not** the same thing. The redirects happen *in order* from left to right. – Jite Jul 14 '15 at 10:01
  • **Correct me if I'm wrong:** `echo hey >/dev/null 2>&1` discards STDOUT and redirects STDERR to STDOUT, means any errors are printed ### `echo hey 2>&1 >/dev/null` redirects STDERR to STDOUT and then discards STDOUT, so nothing is printed – blackjacx Oct 22 '21 at 22:19

5 Answers5

165

To quickly explain what the others missed:

echo "hey" >&2

> redirect standard output (implicit 1>)

& what comes next is a file descriptor, not a file (only for right hand side of >)

2 stderr file descriptor number

Redirect stdout from echo command to stderr. (If you were to useecho "hey" >2 you would output hey to a file called 2)

Jite
  • 4,250
  • 1
  • 17
  • 18
33

The use of >&2 here is sending the output to standard error instead of standard out. This is generally the better place to send logging output that isn't the actual result of the computation, especially if the result is printed to standard out (possibly redirected to a file) rather than to some other file output (i.e. sending the logging to standard error ensures that it won't get included with the real output that was redirected to the output file).

chepner
  • 497,756
  • 71
  • 530
  • 681
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
19

While other answers give good explanations, they're missing the exact question that is being asked here. The best answer is in the form of a comment directly on the question, but alas, Stack Overflow does not consider me worthy of being allowed to add comments.

So, quoting tripleee:

The previse [sic] position of the redirection in the command line is not important. All of >&2 echo message and echo >&2 message and echo message >&2 are equivalent.

This is the exact question that I came looking for, and none of the current answers answer that; they just explain things that I already knew. On the other hand, the question could benefit from better phrasing, but again, I am barred from commenting, so...

Dave.Haku
  • 341
  • 3
  • 5
  • 4
    And I'll just add again then that the above is true for _one_ redirection. Assuming that stdout and stderr points to the tty, then `1>/dev/null 2>&1` and `2>&1 1>/dev/null` is not the same thing. The former redirects both `stdout` and `stderr` to `/dev/null` where as the latter redirects `stderr` to whatever `stdout` was pointing to at the time (the tty) and then redirects `stdout` to `/dev/null`, thus resulting in all `stderr` output still going to the tty. – Jite Aug 01 '19 at 20:31
  • This was the answer I was looking for. Having already understood redirection (`>&2`), I spotted a code `echo >&2 "text" >&2` and wanted to know why that `>&2` was there twice before and after the text. This answer helped me understand that this duplication was a redundant addition. – DFeng Dec 28 '21 at 17:04
10

The >&2 redirection is a shortcut for 1>& 2.

You understand 2>& 1 so you will understand that this links the command's stdout to the current stderr


Footnote about shell redirection syntax...

This looks like

{numfd}[operator]{whitespace}[target]

with the {...} parts optional. The whitespace between operator and target is recommended for readability, but is not required. On the other hand, if you include the {numfd} in front of the operator, there can not be any whitespace between the two of them.

[operator] can be one of <, >, >> for file targets and <&, >& for file descriptor targets.

{numfd} defaults to 0 for <, 1 for > and >>.

[target] is a filename, unless operator ends in &. In that case, it is interpreted as a numeric file descriptor.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
0

command > /yourfile is equal to command 1> /yourfile

so >&2 is same as 1>& 2 redirect stdout to stderr

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
focus zheng
  • 345
  • 1
  • 4
  • 12