The Bash internal variable $PIPESTATUS
does this. It is an array containing the exit status(es) of the commands in the last executed pipe. The first command in the pipe is $PIPESTATUS[0]
, etc:
$ ech hi | while read line; do echo $line; done
-bash: ech: command not found
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
127 0
$ echo hi | while read line; do ech $line; done
-bash: ech: command not found
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
0 127
$ echo hi | while read line; do echo $line; done
hi
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
0 0