On Fri, Dec 31, 2010 at 10:56:35AM +0100, dnade....@orange-ftgroup.com wrote:
> Ha. Indeed, if i use declare ???A, it works.
> 
> But why is bash letting me use foo[bar]=something in the first place, if I 
> don???t declare foo as an associative array ?
> Maybe the bug???s here.

As Pierre said, foo[bar]=something without a declare is making a regular
array, not an associative array.  "bar" is being evaluated in a math
context, which means bash will use the value of the variable "bar" if
it exists and is numeric.  If the value isn't numeric, it will be
treated as yet another variable name, and evaluated, and so on, until
bash either finds a numeric value, or finds an empty string or an invalid
variable name, at which point it gives up and uses 0.

imadev:~$ unset array; foo=bar bar=quux quux=orly orly=42 array[foo]=x; echo 
"${!arr...@]}"
42

Quite a trap for the unwary.

Reply via email to