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: Mike Jonkmans
Subject: Re: PIPESTATUS differs from $? for compound command
Date: Mon, 9 Dec 2024 20:29:13 +0100

On Mon, Dec 09, 2024 at 06:21:53PM +0100, Ulrich Müller wrote:
> 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.
>       
>       If the pipeline consists of more than one command, like
>       "if false; then :; fi | true" or "true | if false; then :; fi"
>       then it behaves as I would expect, i.e. all elements of
>       PIPESTATUS are zero.
> 
> Repeat-By:
>       $ if false; then :; fi
>       $ echo "ret = $?, status = ${PIPESTATUS[*]}"
>       ret = 0, status = 1

This is not a bug, maybe unexpected.
Key considerations:
1) Compound commands, such as 'if...', have statuses.
   For 'if...' that is that of the last command executed (in the body),
   or zero if no condition tested true.
2) PIPESTATUS reports on the statuses of the last (foreground) pipeline.

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.

Both other examples, e.g. 'if false; then :; fi | true',
are, for PIPESTATUS purposes, equivalent to: 'true | true'.

-- 
Regards, Mike Jonkmans



reply via email to

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