However, if your pipe is in a command substitution or other subshell,
PIPESTATUS won't be useful. You'll have to use pipefail.

$ set +o pipefail
$ var=$(false | true)
$ declare -p PIPESTATUS    # shows the status of the assignment, not the "false"
declare -a PIPESTATUS='([0]="0")'
$ var=$(false | true)
$ echo $?
0
$ set -o pipefail
$ var=$(false | true)
$ declare -p PIPESTATUS    # shows any failure
declare -a PIPESTATUS='([0]="1")'
$ var=$(false | true)
$ echo $?
1

Both $? and PIPESTATUS are affected by pipefail in the case of a pipe
in a subshell.

For comparison:

$ set +o pipefail
$ false | true
$ declare -p PIPESTATUS
declare -a PIPESTATUS='([0]="1" [1]="0")'
$ false | true
$ echo $?
0
$ set -o pipefail
$ false | true
$ declare -p PIPESTATUS
declare -a PIPESTATUS='([0]="1" [1]="0")'
$ false | true
$ echo $?
1

pipefail affects $?, but PIPESTATUS works with or without pipefail in
the case of a free-standing pipe.

On Wed, Aug 11, 2010 at 8:53 AM, Eric Blake <ebl...@redhat.com> wrote:
> On 08/11/2010 07:46 AM, Eric Blake wrote:
>> On 08/11/2010 05:20 AM, Pierre Gaston wrote:
>>> On Wed, Aug 11, 2010 at 2:18 PM, Peng Yu <pengyu...@gmail.com> wrote:
>>>> Hi,
>>>>
>>>>
>>>> The following example returns the exit status of the last command in a
>>>> pipe. I'm wondering if there is a way to inherent non-zero exit status
>>>> using pipe. That is, if there is any command in a pipe that return a
>>>> non-zero status, I'd like the whole pipe return a non-zero status.
>>>
>>> set -o pipefail
>>
>> But be aware that you seldom want to use this globally
>
> A less invasive technique is to inspect the array variable
> ${pipestat...@]}, which bash maintains for all elements of the previous
> pipeline.  But remember, both pipefail and $PIPESTATUS are bash
> extensions, and not pure POSIX.
>
> --
> Eric Blake   ebl...@redhat.com    +1-801-349-2682
> Libvirt virtualization library http://libvirt.org
>
>

Reply via email to