File descriptor redirection with echo

Why

echo 'foo' 1>&2 2>/dev/null

      

gives an output? foo

is redirected to file descriptor 2, file descriptor 2 is redirected to /dev/null

. Shouldn't there be a way out?

+3


source to share


1 answer


It's about the order when you do the redirect. When bash sees multiple redirects, it processes them from left to right.

The first stdout redirect redirects to the current value of stderr (tty). When stderr changes to / dev / null, stdout is still written to the previous output of stderrs (tty).

Change the location on these and it will work.



echo 'foo' 2>/dev/null 1>&2

      

See http://www.catonmat.net/blog/bash-one-liners-explained-part-three/ for details

+4


source







All Articles