I told you that would happen...

BASH trick: exit codes for piped commands via $PIPESTATUS


Here's an example pipe:

$ true | false | true | false

$? will return 1 because that is the exit code of the last command in the pipe (false)

If you need to access the exit code of previous commands in the pipe, use the BASH built-in array $PIPESTATUS. For example, to get the exit code of the first command ("true" in this example), use


And so on.

$ true | false | true | false
$ echo ${PIPESTATUS[*]}
0 1 0 1

To save $PIPESTATUS in another variable:

$ true | false | true | false
$ foo=( ${PIPESTATUS[*]} )
$ echo ${foo[*]}
0 1 0 1


stuff and junk

Syndicate content