How do I get the exit code of a process that's piped to tee?
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?
The environment variable $PIPESTATUS
is an array of exit statuses for all processes in a pipeline.
Also use a subshell:
tm@hoegaarden:~$ cat foo.sh
#!/bin/bash
echo "stuff and junk"
exit 123
tm@hoegaarden:~$ (./foo.sh ; echo $? > ./retval ) | tee output
stuff and junk
tm@hoegaarden:~$ cat retval
123