There's a command that always prints stuff to stderr. Such information is never useful except when it doesn't write anything to stdout.

So, is there a simple shell way, tool or command to help filtering stdout and stderr coming from the command and pipe them such that former stderr output is printed to stderr only if former stdout output is empty, otherwise (when stdout is not empty) it just prints stdout?

1 Answer 1


You could redirect them both to temporary files and (in a script) check if anything was written to the standard output.

Something like

mytemp=$(mktemp -d)
trap "rm -rf $mytemp" EXIT INT QUIT HUP
"$@" 2>$mytemp/error | tee $mytemp/output
[ -s $mytemp/output ] || cat $mytemp/error

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .