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: Ulrich Müller
Subject: Re: PIPESTATUS differs from $? for compound command
Date: Wed, 11 Dec 2024 08:44:22 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

>>>>> On Tue, 10 Dec 2024, Chet Ramey wrote:

> On 12/9/24 12:21 PM, Ulrich Müller wrote:
>> Bash Version: 5.2
>> Patch Level: 37
>> Release Status: release
>> Description:
>> For a compound command like "if" or "while" and with
>> an unsuccessful test, the last element of PIPESTATUS is not the
>> return status of the compound but that of the test command.
>> For example, "if false; then :; fi" returns 0 but ${PIPESTATUS[0]}
>> is 1.

> Compound commands don't generally set PIPESTATUS. Multi-element pipelines
> (the original purpose, because otherwise why bother), simple commands
> (single-element pipelines) including shell builtins, and compound commands
> that resemble simple commands (`[[', `((') set it.

> Yes, you can make the case that compound commands are pipelines, and,
> according to the grammar, they are, but their status is always available
> in $?, and they never have more than one status value.

In other words, this was a design choice?

One problem is that error-checking by testing PIPESTATUS for all-zero
doesn't work after a construct like this (and similar for "case"):

  if cond1; then
    foo | bar
  elif cond2; then
    baz
  fi

I'm aware that there's a simple workaround by adding an explicit
"else true" clause. Nevertheless it's not what I would have expected,
when the manual uses the same wording "most recently executed
foreground pipeline" for both $? and PIPESTATUS.



reply via email to

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