help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Destroying arbitrary subset in an associative array?


From: Chet Ramey
Subject: Re: [Help-bash] Destroying arbitrary subset in an associative array?
Date: Sun, 25 Sep 2016 12:15:04 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

On 9/18/16 11:01 PM, Dan Douglas wrote:

> Bash evaluates expansions in array indices dynamically as part of the
> process of resolving its value. Often that means array indices must be
> regarded as unexpanded strings that are dynamically evaluated later.
> 
> That solves this very problem (and many other less obvious ones) in
> about the simplest possible way, at the cost of requiring everyone
> know when to add extra quoting.
> 
>  ~ $ bash /dev/fd/9 9<<\EOF
> typeset -A a=( [\\]= [\"]= [\)]= ) b
> for x in "address@hidden"; do b[$x]=; done
> b+=([\`]= [\]]=)
> typeset -p b
> for x in "address@hidden"; do
>   unset -v 'b[$x]'
> done
> typeset -p b
> EOF

It does, and this was part of the original intent of making all array
accesses use the same set of functions internally.  It's just hard to
explain, and leads to situations where it's very hard to unset array
elements containing quote characters without knowing shell behavior
well enough to understand when to add that extra quoting.

You have to quote the arguments to `unset' somehow anyway to be safe,
since the brackets make the word subject to filename expansion, but by
the time unset gets the arguments it's impossible for it to know whether
or not they've undergone a round of word expansion.  Bash assumes that
they have not been expanded.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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