On 8/9/14, 11:34 AM, Jason Vas Dias wrote:
> Good day bash list -
>
> I don't understand why this emits any output :
> $ ( set -e; declare v=$(false); echo 'Should not get here'; )
> Should not get here
> $
>
> While this does not:
> $ ( set -e; v=$(false); echo 'Should not get here'; )
> $
>
> Shouldn't declare / typeset behave like the normal variable assignment
> statement
> wrt command substitution ? It does not seem to be documented anywhere if
> it is not.
It's straightforward. Setting the `-e' option will cause the shell to exit
if a commsnd returns a non-zero exit status. Look at the exit status
returned by the two commands in question.
The declare builtin returns success unless the assignment itself fails:
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
assign a value to an array variable without using the compound
assignment 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.
The assignment statement, since there is no command name, usually returns
success. Posix thought it useful to have a way to discover whether a
command substitution fails even when an assignment succeeds -- probably an
implementation artifact of a historical shell -- so we have
If there is a command name left after expansion, execution
proceeds as described below. Otherwise, the command exits. If
one of the expansions 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.
(An assignment statement is one of the expansions that is removed before
a command is executed.)
Both of those quotes are from the bash-4.3 manual page.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/