Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

trap question



This script:

typeset -g var=0
test1 ()
{
trap " [[ $var == '0' ]] && echo trap: var is: $var" EXIT
var=0
return
}
test2 ()
{
var=1
test1
echo "test2: var is: $var"
}

run:

$ . test1; test1; test2
trap: var is: 0
test2: 0

... I'm puzzled, even tho test2 does reset the variable to '1', test1 sets it back to '0' just as the final 'echo' reports so I'm expecting the trap to spring.  Foolishly since that's C thinking whereas in interpreted code the value of the variable *at the time of the definition of the trap* is what is in effect and in this case that's '1'.  So what I'm really wanting is some way to set the trap in such a way that it responds to the value of 'var' *at the time of the springing of the trap*, namely at 'return'.  Can it be done?





Messages sorted by: Reverse Date, Date, Thread, Author