Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo >&2 "some text" what does it mean in shell scripting

Tags:

shell

unix

sh

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?

like image 340
CuriousMind Avatar asked May 06 '14 08:05

CuriousMind


People also ask

What you mean by echo?

1 : the repetition of a sound that is caused by reflection of sound waves. 2 : the sound that is due to reflection of sound waves. Other Words from echo. echo verb echoed; echoing\ ˈek-​(ˌ)ō-​iŋ, ˈek-​ə-​wiŋ \

What is echo example?

Echo is defined as a sound repeating by sound wave reflection, having a lasting or far reaching impact, or repeating what someone else has said. An example of echo is the repeating of a sound created by footsteps in an empty marble hallway.

How does a echo work?

Echoes. An echo is a sound that is repeated because the sound waves are reflected back. Sound waves can bounce off smooth, hard objects in the same way as a rubber ball bounces off the ground. Although the direction of the sound changes, the echo sounds the same as the original sound.

Is echo a Scrabble word?

Yes, echo is a valid Scrabble word.


3 Answers

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)

like image 162
Jite Avatar answered Sep 23 '22 13:09

Jite


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

like image 45
Michael Aaron Safyan Avatar answered Sep 23 '22 13:09

Michael Aaron Safyan


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

like image 32
Dave.Haku Avatar answered Sep 21 '22 13:09

Dave.Haku