Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
Get exit code of process that's piped to another

If I pipe the output of one command into another, is there a way for me to check explicitly whether the first command failed?

For example, if prog processes a file and outputs to STDOUT:

prog file1 | cmp - file2

is there a way to check if prog explicitly failed or succeeded (by return code)? I want to avoid the use of temporary files, and don't want to rely on the output of the second command; so, for the example above, I want to avoid examining the output of cmp to determine if prog failed (if there is no affirmative answer to my question, I'll just fall back to doing precisely that).

share|improve this question
add comment

marked as duplicate by Michael Mrozek Jun 1 '12 at 20:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 0 down vote accepted

You want set -o pipefail.

From the Bash manpage:

pipefail

If set, the return value of a  pipeline  is  the value  of
the  last (rightmost) command to exit with a non-zero status,
or zero if all  commands in  the pipeline exit successfully.
This option is disabled by default.
share|improve this answer
    
also have a look at PIPESTATUS –  Ulrich Dangel Jun 1 '12 at 20:02
add comment

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