Bash tip: idiom to get the first error code from a pipePosted: February 23, 2016
real_command | filter_stuff | prettify | do_something_else
The problem arises when you try to figure out if your command succeeded or not. If you `echo $?` you’ll get the return code for the last chain in the pipe. You don’t really care about the output value of do_something_else, do you?
I haven’t found a solution I really like to this problem, but this idiom is handy:
out=`real_command` && echo $out | filter_stuff | prettify | do_something_else
Now $? will hold the value of real_command, and you can actually use it to diagnose the real problem.