Internal Bash Variable – PIPESTATUS

If you aren’t aware, there are a lot of useful internal BASH variables, some of which can be found here:

https://www.tldp.org/LDP/abs/html/internalvariables.html

One of particular use is PIPESTATUS. This can be very useful if you need to pipe a commands output to another command, and you need to check the return code.

For instance:

my_super_command | tee my.log

If my_super_command fails and you print the last return code you will see everything worked just fine:

echo $?

However, if you print the PIPESTATUS[0], then you can see the error:

echo $PIPESTATUS[0]

Advertisements