Date:        Wed, 1 Sep 2021 19:04:12 +0100
    From:        Harald van Dijk <a...@gigawatt.nl>
    Message-ID:  <837d3b5b-ac61-98eb-2741-d667a78e2...@gigawatt.nl>

  | $? is defined as "Expands to the decimal exit status of the most recent 
  | pipeline (see Pipelines)."

I suspect this is just a slightly more precisely specified version of
what the original Bourne shell (7th edition) man page said of $?

        .B ?
        The value returned by the last executed command
        in decimal.

"The value returned" has turned into "exit status" which is
clearly what is intended (not something sent to stdout or whatever),
and "command" has been replaced by "pipeline" (of which a command,
as specified in the 7th edition man page, is just the degenerate case),
which is correct.

The 7th edition man page says nothing about the exit status of a
command substitution, and nothing about commands without command words,
or what exit status they might have.   It also does not say that
command substitutions are executed in a subshell environment.

So one could read its man page as implying that $? is set whenever
a command terminates, including commands for command substititions
(and since those are, and always were, actually executed in a subshell
environment, the final status of that environment instead of the individual
commands (or pipelines) that form the command substitution).

Further, we now know from what has been reported here in other messages
that that was how the implementation worked (I assume, that whenever it
did a wait(2) for a child, it set $? to the exit status (or signal number
modified so that can be detected) (as well as generating the exit status
from the built-in commands).

It seems likely that when this was all specified for the older POSIX/SUS
standards, the wording for $? was taken from the sh man page, corrected
as much as was known it should be, and simply then forgotten.   That is,
there was probably never any intent to explicitly say that $? can only be
set when a pipeline in the current execution environment terminates (and
is waited upon), that most probably simply is the way things ended up written.

Now, because of that, we can no longer reasonably say that $? is set
as each command substitution is performed, and then (normally) overridden
by the exit status of the command (if there is one), as the current
wording, with its (probably unintended) implications has been around for
too long now - even if it didn't match shells of the time.   But we can
say that in the presence of command substitutions, when the value of $?
used in the same command line is unspecified (used elsewhere there's no
issue).

kre

  • Re: $? in a simple co... Robert Elz via austin-group-l at The Open Group
    • Re: $? in a simp... O─čuz via austin-group-l at The Open Group
    • Re: $? in a simp... Harald van Dijk via austin-group-l at The Open Group
    • Re: $? in a simp... Robert Elz via austin-group-l at The Open Group
      • Re: $? in a ... Chet Ramey via austin-group-l at The Open Group
        • Re: $? i... Joerg Schilling via austin-group-l at The Open Group
          • Re: ... Scott Lurndal via austin-group-l at The Open Group
            • ... Joerg Schilling via austin-group-l at The Open Group
          • Re: ... Chet Ramey via austin-group-l at The Open Group
    • Re: $? in a simp... Robert Elz via austin-group-l at The Open Group

Reply via email to