bug-bash
[Top][All Lists]
Advanced

[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: Sun, 19 Mar 2017 18:05:19 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

On 3/19/17 5:51 PM, Stephane Chazelas wrote:

> On comp.unix.shell ot http://unix.stackexchange.com, I've posted
> many articles describing how to do splitting in POSIX-like
> shells:
> 
> ( # subshell for local scope
>   unset -v  IFS # restore default splitting behaviour
>   set -o noglob # disable globbing
>   cmd -- $var   # split+glob with default IFS and glob disabled
> )
> 
> I'm now considering adding a note along the lines of:
> 
>   "Beware that with current versions of bash, pdksh and yash,
>   the above may not work if used in scripts that otherwise use
>   typeset/declare/local on $IFS or call a function with
>   `IFS=... my-function' (or IFS=... eval... or IFS=...
>   source...)"

You can, of course, do whatever you want.  You might want to read my
message from yesterday about what happens when you do that, or look
at the following examples, after which you may decide that the situation
is not as dire.

$ cat x2
function foo
{
(
        unset -v IFS
        recho "${IFS-unset}"
)
}

IFS=':|'
foo
echo after IFS = "$IFS"
$ ../bash-4.4-patched/bash ./x2
argv[1] = <unset>
after IFS = :|

$ cat x2a
function foo
{
(
        unset -v IFS
        recho "${IFS-unset}"

        foo='a:b:c:d'
        recho $foo
)
}

IFS=':|'
foo
echo after IFS = "$IFS"
$ ../bash-4.4-patched/bash ./x2a
argv[1] = <unset>
argv[1] = <a:b:c:d>
after IFS = :|

or

$ cat x2b
function foo
{
        typeset IFS='+'
        unset -v IFS
        recho "${IFS-unset}"
}

IFS=':|'
foo
echo after IFS = "$IFS"
$ ../bash-4.4-patched/bash ./x2b
argv[1] = <unset>
after IFS = :|

or even

$ cat x2c
function foo
{
        typeset MIFS='+'
        unset -v MIFS
        recho "${MIFS-unset}"
}

MIFS=':|'
foo
echo after MIFS = "$MIFS"
$ ../bash-4.4-patched/bash ./x2c
argv[1] = <unset>
after MIFS = :|

-- 
``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/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]