[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: declare XXX=$(false);echo $?
From: |
Dale R. Worley |
Subject: |
Re: declare XXX=$(false);echo $? |
Date: |
Fri, 02 Dec 2022 11:39:12 -0500 |
Chet Ramey <chet.ramey@case.edu> writes:
> On 12/2/22 5:28 AM, Ulrich Windl wrote:
>> Surprisingly "declare XXX=$(false);echo $?" outputs "0" (not "1")
>> There is no indication in the manual page that "declare" ignores the
>exit code of commands being executed to set values.
>
> Why do you think it should? `declare' has a well-defined return status.
There it is, end of "SIMPLE COMMAND EXPANSION":
If there is a command name left after expansion, execution proceeds as
described below. Otherwise, the command exits. If one of the expanâ
sions contained a command substitution, the exit status of the command
is the exit status of the last command substitution performed. If
there were no command substitutions, the command exits with a status of
zero.
and:
declare [-aAfFgiIlnrtux] [-p] [name[=value] ...]
typeset [-aAfFgiIlnrtux] [-p] [name[=value] ...]
[...]
The return value
is 0 unless an invalid option is encountered, an attempt is made
to define a function using ``-f foo=bar'', an attempt is made to
assign a value to a readonly variable, an attempt is made to asâ
sign a value to an array variable without using the compound asâ
signment syntax (see Arrays above), one of the names is not a
valid shell variable name, an attempt is made to turn off readâ
only status for a readonly variable, an attempt is made to turn
off array status for an array variable, or an attempt is made to
display a non-existent function with -f.
If you input "XXX=$(false)", there isn't a command name, it's a sequence
of assignments, and "the exit status of the command is the exit status
of the last command substitution performed". But if you input "declare
XXX=$(false)", you're executing the "declare" command, and the exit
status doesn't depend on the command substitution.
Dale
Re: declare XXX=$(false);echo $?, Martin D Kealey, 2022/12/02