Re: 'declare' does not honor '-e' in command substituted assignments - a bug ?

2014-08-10 Thread Chet Ramey
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/



'declare' does not honor '-e' in command substituted assignments - a bug ?

2014-08-09 Thread Jason Vas Dias
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.

I'm using bash-4.3.18(1)-release ,  compiled from GIT under RHEL 6.4
(gcc-4.4.7)
for x86_64 - I've also tested the default RHEL 6.4 bash-4.1.2(1)-release and the
latest  4.3.22(1)-release with the same results.

Actually , this problem seems to apply to all built-ins -
 $ ( set -e ; echo $(false); echo 'not ok')

 not ok
 $

I can't seem to find this behaviour documented anywhere . The same behaviour
happens in posix mode .

I'd appreciate an explanation as to why this behavior is not a bug .

Thanks  Regards,
Jason


test_-e.sh
Description: Bourne shell script


Re: 'declare' does not honor '-e' in command substituted assignments - a bug ?

2014-08-09 Thread Dan Douglas
On Saturday, August 09, 2014 04:34:11 PM 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'; )
  $

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01

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.

The declaration commands return the status of the command itself, which masks
the status of assignments. To get the status, either perform the declaration
and assignment separately, or use a temporary variable to store the status. For
set -e the former is the only option.

-- 
Dan Douglas