bug-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] difference of $? and ${PIPESTATUS[0]}


From: Chet Ramey
Subject: Re: [Help-bash] difference of $? and ${PIPESTATUS[0]}
Date: Mon, 22 Apr 2024 15:49:04 -0400
User-agent: Mozilla Thunderbird

On 4/22/24 2:13 AM, felix wrote:

I could explain that '$?' is result of bash's if...then...fi group command
executed correctly and PIPESTATUS hold result of "most-recently-executed
foreground pipeline", but man page say:

     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).

      ?   Expands  to  the exit status of the most recently executed fore‐
          ground pipeline.

It's clumsy wording, no doubt, but the two aren't intended to be the same
or necessarily have the same value.

The language in the description of $? is very close to POSIX, since the
POSIX grammar basically makes any command a pipeline. It's formal so the
standard can relate it to the grammar, but very few people think of

if a; then echo b; else echo c; fi

as a `pipeline'.


If so, "$?" have to be equivalent to "${PIPESTATUS[0]}", I think.

No, even if this were true, it would be the last member of PIPESTATUS,
modulo the `pipefail' setting.

I suggest that man page should be modified to replace "foreground pipeline"
by "command" under "?" paragraph.

Yeah, that's probably closer to the real meaning. The description of
PIPESTATUS refers to pipelines as described in Pipelines under SHELL
GRAMMAR (which can also be a Simple Command from the same section).

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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