Linix tip - stderr skips pipes

July 19, 2010 by

What does this line of bash output (and where?):

echo error > /dev/stderr | cat 2>/dev/null

I print “error” to stderr and pipe to cat while sending stderr to the abyss, right? Yes, I do… the pipe doesn’t grab stderr. Check this out:

echo error > /dev/stderr | cat >&/dev/null

You still see “error” being printed out, because cat isn’t doing it. The pipe doesn’t pick up stderr, only stdout. If you need both (like in cron or if you want to mail the output of commands), be sure to use >& when redirecting:

echo error 2>&1 | mail -s "automated emailz" [email protected]

The moral of the story is that stderr is for errors. For stuff like “HOLY SHIT THIS BROKE ALL KINDS OF SHIT!” which is very helpful to see immediately, not when the rest of the pipes finish reading/writing. So if you want to pass stderr along, either rethink what you’re doing, or use 2>&1.


Made by Adam for himself at addumb.com
:wq