bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PIPESTATUS differs from $? for compound command


From: Dale R. Worley
Subject: Re: PIPESTATUS differs from $? for compound command
Date: Tue, 10 Dec 2024 12:07:32 -0500

"Dale R. Worley" <worley@alum.mit.edu> writes:
>> $ if false; then :; fi | true; echo ${PIPESTATUS[*]}
>> 0 0

Trying to dissect this:

There are *three* pipelines that are executed:
1) 'if false; then :; fi | true'
2) 'false' (which is a pipeline, which is a list, which is the
conditional of the 'if')
3) 'echo ${PIPESTATUS[*]}'
The ':' is a pipeline but it isn't executed.
Note the treachery that (2) is entirely contained within (1), both
syntactically and in the time period of its execution.

The documentation (bash 5.1.0(1), yes I know it's really old) says:
       PIPESTATUS
              An  array  variable (see Arrays below) containing a list of exit
              status values from the processes in  the  most-recently-executed
              foreground pipeline (which may contain only a single command).

Note also that if all of the conditions of an 'if' are false, and it has
no 'else' clause, the 'if' exits with 0, that is, *true*.  That is
counter-intuitive to me and so I misread the documentation of that the
first time.

However adjective "most-recently-executed" is rather ambiguous.  In this
case:
A) The pipeline whose execution was most recently *started* is 'echo
${PIPESTATUS[*]}'.
B) The pipeline whose execution was most recently *completed* is 'if
false; then :; fi | true'.
C) The pipeline whose execution *has been completed* which was most
recently *started* is 'false'.

The evidence doesn't seem to be consistent with any of these three
definitions:

    $ if false; then :; fi | false; echo ${PIPESTATUS[*]}
    0 1
    $ if false; then :; fi | true; echo ${PIPESTATUS[*]}
    0 0
    $ if false; then :; fi; echo ${PIPESTATUS[*]}
    1
    $

Note the difference in the behavior of the 2nd and 3rd examples.

Dale



reply via email to

[Prev in Thread] Current Thread [Next in Thread]