Bash – How to get the exit code of a process that’s piped to tee

bashmac-osxshell

I have bash code like this (Mac OS X):


foo.sh | tee foo.log
echo $?

The problem is that $? contains the exit code of tee and not the exit code of foo.sh. How do I get the exit code of foo.sh?

Best Answer

The environment variable $PIPESTATUS is an array of exit statuses for all processes in a pipeline.