Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have bash code like this (Mac OS X): | tee foo.log echo $?

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

share|improve this question

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

share|improve this answer
Why'd you ask the question if you know the answer? – Patrick Aug 12 '12 at 19:36
Probably for the same reason there's a button to "answer your own question". – Skaperen Aug 12 '12 at 20:03
@Patrick I didn't know the answer when I asked the question, but did some more digging and found it. – bluesmoon Aug 13 '12 at 0:44

Also use a subshell:

tm@hoegaarden:~$ cat

echo "stuff and junk"

exit 123
tm@hoegaarden:~$ (./ ; echo $? > ./retval ) | tee output
stuff and junk
tm@hoegaarden:~$ cat retval 
share|improve this answer
This is more portable (not specific to bash) - hence upvote. – FooF Aug 14 '12 at 8:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.