[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: PIPESTATUS differs from $? for compound command
From: |
Mike Jonkmans |
Subject: |
Re: PIPESTATUS differs from $? for compound command |
Date: |
Mon, 9 Dec 2024 23:20:38 +0100 |
On Mon, Dec 09, 2024 at 09:20:54PM +0100, Andreas Schwab wrote:
> On Dez 09 2024, Mike Jonkmans wrote:
> > So, with 'if false; then :; fi', the whole if has 0 as status, due to #1.
> > The last pipeline is the 'false' command. So $PIPESTATUS = 1.
> Why is `if false; then :; fi' not a pipeline? It is a command, and the
> components of a pipeline are commands.
It is a pipeline indeed, but not the last (doc says: most-recently-executed).
The last is the 'false' simple command/pipeline.
> > Both other examples, e.g. 'if false; then :; fi | true',
> > are, for PIPESTATUS purposes, equivalent to: 'true | true'.
> Why is `if false; then :; fi' not equivalent to `true' above?
Not sure why you are asking this.
$ if false; then :; fi && echo true
true
Because if statements have status true when the conditions are false
(multiple conditions in the case of elif's),
But the PIPESTATUS refers to the 'false' pipeline:
$ if false; then :; fi; echo ${PIPESTATUS[*]}
1
More clearly:
$ if (exit 42); then :; fi; echo ${PIPESTATUS[*]}
42
With an explicit pipeline after the if:
$ if false; then :; fi | true; echo ${PIPESTATUS[*]}
0 0
In this case the most-recently-executed pipeline is the 'if...fi | true'.
--
Regards, Mike Jonkmans