Hi
    Just a quick query re. the a behavior of trap when called from function, 
not sure if it's a bug or inconsistency or intentional.

    Basically it seems (without set -o errtrace)

-          an ERR trap can be set from within a function when no ERR trap is 
currently defined.

-          ERR trap can then not be changed or cleared unless cleared from 
global scope (ie cannot be cleared within a function).

-          With set -o errtrace, the subsequent calls do succeed in changing 
the trap, as might be expected.

    I'm just puzzled at the fact that I can set the ERR trap within a function 
the first time, but not subsequently.

   Example script and output below

Thanks
David



Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-redhat-linux-gnu' 
-DCONF_VENDOR='redhat' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib  -D_GNU_SOURCE -DRECYCLES_PIDS 
-DDEFAULT_PATH_VALUE='/usr/local/bin:/usr/bin'  -O2 -g -pipe -Wall 
-Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong 
--param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic
uname output: Linux oam02.bfs.openwave.com 3.10.0-229.el7.x86_64 #1 SMP Thu Jan 
29 18:37:38 EST 2015 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

Bash Version: 4.2
Patch Level: 46
Release Status: release

Example script and output :

  oam02$ cat test.sh
#!/bin/bash

trap_1() {
   trap  'echo this is trap 1' ERR
}

trap_off() {
   trap ''  ERR
}

trap_2() {
   trap 'echo this is  trap 2'  ERR
}


trap_1
trap -p ERR

trap_2
trap -p ERR

trap_off
trap_2
trap -p ERR

trap ""  ERR
trap_2
trap -p ERR

oam02$ ./test.sh
trap -- 'echo this is trap 1' ERR
trap -- 'echo this is trap 1' ERR
trap -- 'echo this is trap 1' ERR
trap -- 'echo this is  trap 2' ERR

Reply via email to