On 05/04/2023 15:35, Chet Ramey via austin-group-l at The Open Group wrote:
On 4/5/23 9:06 AM, Martijn Dekker via austin-group-l at The Open Group wrote:
Consider:

     false || echo $(true) $?

dash, mksh and yash print 1.
bash, ksh93 and zsh print 0.
Which is right?

I believe dash, mksh, yash are already right based on the current wording of the standard. As Martijn wrote, the rule is that $? "Expands to the decimal exit status of the most recent pipeline", the most recent pipeline in the shell environment in which $? is evaluated is "false", and changes in the subshell environment shall not affect the parent shell environment, including changes in the subshell environment to $?.

A variant with slightly different semantics:

(exit 8)
a=4 b=$(exit 42) c=$?

Likewise, the most recent pipeline in the shell environment in which c=$? is evaluated is "(exit 8)", so c should be set to 8.

echo status:$? c=$c

Here, as shells agree, $? should expand to 42.

The standard is clear about what $? should be for the echo, but should it
be set fron the command substitution for the assignment to c?

The way the standard specifies that the final $? should expand to 42 is in 2.9.1 Simple Commands:

> [...] If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of the last command substitution performed. Otherwise, the command shall complete with a zero exit status.

That is, the command substitution itself does not cause $? in the parent environment to change, rather it is the fact that the entire "a=4 b=$(exit 42) c=$?" command exits with status 42 that causes $? to change. Nothing else in the standard indicates that the parent shell environment's $? value changes; while the expansion of the second command is in place, "the most recent pipeline" is still the previous one.

You have linked to <https://www.austingroupbugs.net/view.php?id=1150> in a subsequent reply. This does not see the current wording as sufficiently clear, but does clear up with new wording that regardless of whether this can be inferred from the current wording of the standard, this interpretation is the intended one.

Cheers,
Harald van Dijk

  • Re: $? behaviour aft... Chet Ramey via austin-group-l at The Open Group
    • Re: $? behaviou... Oğuz via austin-group-l at The Open Group
      • Re: $? beha... Chet Ramey via austin-group-l at The Open Group
      • Re: $? beha... Harald van Dijk via austin-group-l at The Open Group
        • Re: $? ... Oğuz via austin-group-l at The Open Group
          • Re:... Harald van Dijk via austin-group-l at The Open Group
            • ... Steffen Nurpmeso via austin-group-l at The Open Group
              • ... Steffen Nurpmeso via austin-group-l at The Open Group
            • ... Oğuz via austin-group-l at The Open Group
            • ... Harald van Dijk via austin-group-l at The Open Group
    • Re: $? behaviou... Harald van Dijk via austin-group-l at The Open Group
      • Re: $? beha... Chet Ramey via austin-group-l at The Open Group
        • Re: $? ... Harald van Dijk via austin-group-l at The Open Group
          • Re:... Chet Ramey via austin-group-l at The Open Group
            • ... Harald van Dijk via austin-group-l at The Open Group
      • Re: $? beha... Robert Elz via austin-group-l at The Open Group
        • Re: $? ... Chet Ramey via austin-group-l at The Open Group
    • Re: $? behaviou... Robert Elz via austin-group-l at The Open Group

Reply via email to