Bash: Unset

From FVue
Jump to: navigation, search

It appears that `unset' is capable of traversing down the call-stack and unsetting variables repeatedly:

a=0 b=0 c=0 d=0 e=0
_unset() { unset -v b c c d d d e; }
t1() {
    local a=1 b=1 c=1 d=1
t2() {
    local a=2 b=2 c=2 d=2 e=2
    echo a:$a b:$b c:$c d:$d e:$e
t1  # Outputs: a:2 b:1 c:0 d: e:0
    #            ^   ^   ^   ^  ^-- unset once (skipped t1)
    #            |   |   |   +----- unset thrice to global
    #            |   |   +--------- unset twice till global
    #            |   +------------- unset once till t1
    #            +----------------- unset not

This behaviour of `unset' resembles the capabilities of tcl's `upvar' and makes it possible to pass variables by reference in bash.

See also
Behaviour questioned on bug-bash mailing list


blog comments powered by Disqus