Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read return code of command when piped with another command [duplicate]

Tags:

linux

bash

shell

I have a bash script where I execute a command and tee to a file. On checking the return code, it is always 0 which is for the tee <> command.

make all | tee output.log
if [[ $? -ne 0 ]]; then
    echo "Make failed"
    exit 1
else
    blah blah
fi

Is there a way to check the return code of the first command (i.e. make all in this case)?

like image 229
oak1208 Avatar asked Sep 14 '26 16:09

oak1208


1 Answers

Suppose you have the commands pipe command1 | command2, you can get each command exit code by:

echo "${PIPESTATUS[0]} - ${PIPESTATUS[1]}"
like image 132
Antonio Petricca Avatar answered Sep 16 '26 07:09

Antonio Petricca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!