July 19, 2010
What does this line of bash output (and where?):
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:
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:
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.