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

CuriousMind picture CuriousMind · May 6, 2014 · Viewed 48.3k times · Source

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?

Answer

Jite picture Jite · May 6, 2014

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)