Date:        Wed, 5 Apr 2023 10:35:58 -0400
    From:        "Chet Ramey via austin-group-l at The Open Group" 
<austin-group-l@opengroup.org>
    Message-ID:  <ad50dd74-a0dc-8fab-e416-917e45768...@case.edu>

  | A variant with slightly different semantics:
  |
  | (exit 8)
  | a=4 b=$(exit 42) c=$?
  | echo status:$? c=$c
  |
  | 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?

It isn't really different semantics, it is the same thing.   The exit status
from the command substitution in that case is used as the exit status for the
empty command that is line 2 (you're right, that is clear).  But that command
doesn't get to set an exit status until it finishes, and it can't do that
until its associated var assigns have all been performed, which (even
leaving aside the question of the order in which they, and the args for
them, are processed) cannot possibly be before c=$? is expanded and assigned.

Needless to say, the same (exact) set of shells which produced N:N in the
example in my previous message, set c to 42, and all the rest (including
the older ksh93) set c to 8 (which really is what it should be - the other
possibility here would be "unspecified" as even if the exit status were to
become available in the middle of evaluating the args for a command, here
we don't know whether c= or b= will be evaluated first.

All the standard actually says is:

        4. Each variable assignment shall be expanded for tilde expansion,
           parameter expansion, command substitution, arithmetic expansion,
           and quote removal prior to assigning the value.

There's nothing there about the order in which they're processed (unlike,
for example, redirects, which are required to be process left to right)
which makes the order implicitly unspecified.   Anything is possible.

But as, in any sane implementation, assigning the values to the variables
should not in any way affect the values assigned to other variables in the
same set of var assigns, it really should not matter the order in which
they're processed, unless someone is idiotic enough to write

           a=1 a=2 a=3

in which case what value gets left in a is anyone's guess, and they get
what they deserve.

kre



        • 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
      • Re: $? beha... Steffen Nurpmeso via austin-group-l at The Open Group
      • Re: $? beha... Robert Elz via austin-group-l at The Open Group
        • Re: $? ... Oğuz via austin-group-l at The Open Group
        • Re: $? ... Robert Elz via austin-group-l at The Open Group
  • Re: $? behaviour aft... Robert Elz via austin-group-l at The Open Group

Reply via email to