I’m a Systems/Software Engineer in the San Francisco Bay Area. I moved from Columbus, Ohio in 2007 after getting a B.S. in Physics from the Ohio State University. I'm married, and we have dogs.

Under my github account (https://github.com/addumb): I open-sourced python-aliyun in 2014, I have an outdated python 2 project starter template at python-example, and I have a pretty handy “sshec2” command and some others in tools.

Linix tip - stderr skips pipes

July 19, 2010

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.


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States License. :wq