Home / Programming / Bash / Viewing Bash Exit Status Codes With Pipes

Viewing Bash Exit Status Codes With Pipes

Recently I was executing bash scripts from within bash scripts, and executing commands based off of exit code, typically I just use

/bin/bash ./script.sh || echo "script.sh failed" 

or just the typical

/bin/bash ./script.sh
if [ $? -ne 0 ]; then echo "script.sh failed"; fi

But now what if you are also piping the output of script.sh through tee? Then you will see that $? is actually the exit code of the tee command.

I found a nifty bash variable/array, $PIPESTATUS. This is an array that contains the exit statuses of all of the exit codes ran by the last command.

By default, if you just echo $PIPESTATUS, you will get the first value, which in this case would be the exit value of /bin/bash ./script.sh.

The exit code of the scripts/commands are placed into the $PIPESTATUS array in the same order that they are executed. Heres an example of how to properly access the exit codes:

who | wc -l | foo

if [ ${PIPESTATUS[0]} -ne "0" ]; then
 echo "The 'who' command failed"
elif [ ${PIPESTATUS[1]} -ne "0" ]; then
 echo "The 'wc -l' command failed"
elif [ ${PIPESTATUS[2]} -ne "0" ]; then
 echo "The 'foo' command failed"
 echo "it worked!"

About J

Welcome to my little corner of the InterWebs! Most of what I post on LinuxDigest is about either automation, something I find interesting, or something I just learned myself. If you want me to post an article about something, just let me know! Im more than happy to help and teach others Linux.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *