[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "unset var" pops var off variable stack instead of unsetting it
From: |
Chet Ramey |
Subject: |
Re: "unset var" pops var off variable stack instead of unsetting it |
Date: |
Sat, 18 Mar 2017 13:16:56 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 |
On 3/17/17 5:51 PM, Stephane Chazelas wrote:
> Now, if that "split" functions is called from within a function
> that declares $IFS local like:
[...]
> because after the "unset IFS", $IFS is not unset (which would
> result in the default splitting behaviour) but set to ":" as it
> was before "bar" ran "local IFS=."
This is how local variables work. Setting a local variable shadows
any global with the same name; unsetting a local variable reveals the
global variable (or really, because bash has dynamic scoping, the value
at a previous scope) since there is no longer a local variable to shadow
it.
>
> A simpler reproducer:
>
> $ bash -c 'f()(unset a; echo "$a"); g(){ local a=1; f;}; a=0; g'
> 0
>
> Or even with POSIX syntax:
>
> $ bash -c 'f()(unset a; echo "$a"); a=0; a=1 eval f'
> 0
In this case, the unset unsets the variable provided in the builtin's
`environment'. It's essentially the same thing.
>
> A work around is to change the "split" function to:
>
> split() (
> local IFS
> unset -v IFS # default splitting
> set -o noglob # disable glob
>
> set -- $1 # split+(no)glob
> [ "$#" -eq 0 ] || printf '<%s>\n' "$@"
> )
>
> For some reason, in that case (when "local" and "unset" are
> called in the same function context), unset does unset the
> variable.
There's special code in bash to preserve the locally-declared nature of
a variable if you use local and unset in the same function scope. That's
been in bash forever (at least as far back as bash-2.0, which is when I
quit looking), for compatibility -- that's how ksh93 behaves -- and user
expectations.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
- Re: "unset var" pops var off variable stack instead of unsetting it, (continued)
- Re: "unset var" pops var off variable stack instead of unsetting it, Eric Blake, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/21
- Re: "unset var" pops var off variable stack instead of unsetting it, Martijn Dekker, 2017/03/22
Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/18
Re: "unset var" pops var off variable stack instead of unsetting it,
Chet Ramey <=
Re: "unset var" pops var off variable stack instead of unsetting it, Peter & Kelly Passchier, 2017/03/19
Re: "unset var" pops var off variable stack instead of unsetting it, 渡邊裕貴, 2017/03/20
Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/20
Re: "unset var" pops var off variable stack instead of unsetting it, Greg Wooledge, 2017/03/20