bug-bash
[Top][All Lists]
Advanced

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

Re: unsetting associative array executes commands


From: Chet Ramey
Subject: Re: unsetting associative array executes commands
Date: Thu, 11 Mar 2021 15:20:00 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.8.0

On 3/11/21 10:06 AM, Jason A. Donenfeld wrote:
This behavior is quite surprising:

The idea is that array subscripts undergo a uniform set of expansions when
they're used, no matter the context. Builtins already undergo word expansions as part of command execution. The result is double expansion
in certain cases (there are more; this is a staple of discussion here on
the list).

You can use the `assoc_expand_once' shell option to reduce the number of
expansions; it would have prevented the behavior you observed here, for
instance. But it's not perfect, as many people on the list will tell you.


$ declare -A blah
$ blah['$(DOESNOTEXIST)']=broken
$ for i in "${!blah[@]}"; do echo "$i"; done
$(DOESNOTEXIST)
$ for i in "${!blah[@]}"; do unset blah["$i"]; done
bash: DOESNOTEXIST: command not found
bash: unset: [$(DOESNOTEXIST)]: bad array subscript


$ cat x1
declare -A blah
blah['$(DOESNOTEXIST)']=broken
shopt -s assoc_expand_once
for i in "${!blah[@]}"; do unset blah["$i"]; done
declare -p blah
$ ../bash-5.1-patched/bash ./x1
declare -A blah=()


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



reply via email to

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