bug-bash
[Top][All Lists]
Advanced

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

Re: List of keys of an associative array


From: Greg Wooledge
Subject: Re: List of keys of an associative array
Date: Mon, 3 Jan 2011 08:15:58 -0500
User-agent: Mutt/1.4.2.3i

On Fri, Dec 31, 2010 at 10:56:35AM +0100, dnade.ext@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 
"${!array[@]}"
42

Quite a trap for the unwary.



reply via email to

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