Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    

2 Answers 2

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 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
share|improve this answer
    
This is more portable (not specific to bash) - hence upvote. –  FooF Aug 14 '12 at 8:45

Your Answer

 
discard

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.