A NOTE has been added to this issue. 
====================================================================== 
https://www.austingroupbugs.net/view.php?id=1309 
====================================================================== 
Reported By:                kre
Assigned To:                
====================================================================== 
Project:                    1003.1(2016)/Issue7+TC2
Issue ID:                   1309
Category:                   Shell and Utilities
Type:                       Enhancement Request
Severity:                   Objection
Priority:                   normal
Status:                     New
Name:                       Robert Elz 
Organization:                
User Reference:              
Section:                    2.9.4 
Page Number:                2371-4 
Line Number:                75726-31 
Interp Status:              --- 
Final Accepted Text:         
====================================================================== 
Date Submitted:             2019-12-19 02:26 UTC
Last Modified:              2020-01-20 18:37 UTC
====================================================================== 
Summary:                    Clarity needed for initial value of $? at start of
compound-list compound statements
======================================================================
Relationships       ID      Summary
----------------------------------------------------------------------
related to          0001150 exit status of command substitution not...
related to          0000051 sh exit status not clear for built-in t...
====================================================================== 

---------------------------------------------------------------------- 
 (0004743) kre (reporter) - 2020-01-20 18:37
 https://www.austingroupbugs.net/view.php?id=1309#c4743 
---------------------------------------------------------------------- 
This (https://www.austingroupbugs.net/view.php?id=1309#c4742) is mostly all good
(the two minor issues I have with it
are
just below) but note that this is really a side issue that developed from
this bug report, I still believe that it would be good to add (and
certainly
harmless) some words to the effect of

    When the list begins execution, the value of the special parameter ?
    shall be that produced by evaluation of the condition-list that
    immediately preceded this execution of the list

applied to if/while/until statements (or somewhere common and stated to
apply
to all such commands) - that is, to clarify that even though the exit
status
($?) from the condition list is not available after one of these compound
commands completes (regardless of being the last command executed as part
of
the compound command) its exit status is available inside the code that is
the
body of the compound command.



My reservations with the wording in the note relate first to this part

    Note: In <tt>var=$(some_command); echo $?</tt> the output is the exit
    status of <tt>some_command</tt>,

which is all fine, until

    its exit status becomes the exit status of the assignment command   
    <tt>var=$(some_command)</tt>

There is no such thing as an "assignment command".  What there is is
variable assignments, and null commands,   What 2.9.1  says is ...

    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.

which does not invent an "assignment command" - but even if it did would
not cover cases like

    umask 0; >/tmp/foo;   #  this is just to create a known environment
    </tmp/foo$(exit 1)

which in all shells except bash and zsh (all I have tested anyway) results
in $? being set to 1 - since on that 2nd line, there is no command name,
but
there is a command substitution, so the status of the last of those (here
there
is just one) becomes the status of this empty command (as 2.9.1 directs).

2.9.1 also says:

    If there is no command name, any redirections shall be performed in a
    subshell environment; it is unspecified whether this subshell
environment
    is the same one as that used for a command substitution within the
command.

which, since it says "unspecified" might seem to be relevant, but I don't
believe it is, whether the redirect happens in the same subshell as the
command substitution isn't really relevant, there is no command name
(except exit, which is the command substitution command) and so the exit
status here should be the status of the (last) command substitution
performed.

Anyway, inventing a fictional assignment command doesn't help, and I don't
think is needed - I don't believe any reference to
     x=$(whatever)
is needed in this context at all.   What is the exit status of various
commands
(including simple commands without command names, which is what this is)
is
specified elsewhere, and doesn't need to also be specified in the
definition
of the ? special parameter.



Second reservation: there is no need for

    When a subshell environment is created, it is unspecified whether the
    value of the special parameter '?' from the invoking shell environment
    is preserved in the subshell or the value is reset to 0.

It is preserved, and if we need say anything at all (I have no issues with
saying something here, more clarity is helpful) it should be

    When a subshell environment is created, the value of the special
parameter
    ? shall initially be the value it had in the shell environment
immediately
    preceding the creation of the subshell environment.

(or words something like that which better meet the standard language).
Perhaps just:

    Creating a subshell environment does not alter the value of the
    special parameter ?

Joerg already cleared up the apparent issue with ksh and the strange 0
status from the test - it is not $? that is incorrect there, but "exit"
and that is an obvious bug, and not worthy of any mention in the standard
at all.

Note that all shells print 1 for

    (exit 1); (echo $?)

including ksh93 (and I presume ksh88).   That makes it clear that it is
not the subshell environment that is causing the 0 from

    (exit 1); (exit) ; echo $?

in ksh, but a bug in the exit command (my guess would be that it is
attempting to do what the standard seems to say, looking for the status of
the "last command" and failing to find one in its current environment,
and consequently defaulting to 0.   That's bogus, but practically
harmless,
as no-one in real code writes a subshell in which the first command is
exit (without a specific exit value) as all that would be is an expensive
no-op. 

Issue History 
Date Modified    Username       Field                    Change               
====================================================================== 
2019-12-19 02:26 kre            New Issue                                    
2019-12-19 02:26 kre            Name                      => Robert Elz      
2019-12-19 02:26 kre            Section                   => 2.9.4           
2019-12-19 02:26 kre            Page Number               => 2371-4          
2019-12-19 02:26 kre            Line Number               => 75726-31        
2020-01-16 17:42 geoffclare     Note Added: 0004731                          
2020-01-16 17:43 geoffclare     Note Edited: 0004731                         
2020-01-16 20:35 kre            Note Added: 0004732                          
2020-01-16 21:36 kre            Note Added: 0004733                          
2020-01-17 04:17 kre            Note Added: 0004734                          
2020-01-17 04:19 kre            Note Edited: 0004734                         
2020-01-17 09:56 joerg          Note Added: 0004735                          
2020-01-17 10:31 kre            Note Added: 0004736                          
2020-01-17 15:39 geoffclare     Note Added: 0004737                          
2020-01-17 15:53 joerg          Note Added: 0004738                          
2020-01-17 15:56 joerg          Note Edited: 0004738                         
2020-01-17 16:04 joerg          Note Edited: 0004738                         
2020-01-17 16:17 geoffclare     Note Edited: 0004737                         
2020-01-18 02:38 kre            Note Added: 0004739                          
2020-01-20 11:57 geoffclare     Note Added: 0004741                          
2020-01-20 14:54 geoffclare     Note Added: 0004742                          
2020-01-20 14:55 geoffclare     Note Edited: 0004742                         
2020-01-20 14:58 geoffclare     Relationship added       related to 0001150  
2020-01-20 15:04 geoffclare     Relationship added       related to 0000051  
2020-01-20 18:37 kre            Note Added: 0004743                          
======================================================================


Reply via email to