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

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


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)


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


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