bug-bash
[Top][All Lists]
Advanced

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

Re: Unsetting all elements of an associative array


From: Greg Wooledge
Subject: Re: Unsetting all elements of an associative array
Date: Wed, 4 Feb 2015 08:39:51 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Feb 04, 2015 at 09:12:12AM +0100, isabella parakiss wrote:
> I'm trying to use unset array[@] to empty an associative array, but something
> goes wrong.

What caused you to believe that would work?

> This produces an error:
> $ declare -A array; unset array[@]; array[path/directory]=value
> bash: path/directory: division by 0 (error token is "directory")

unset 'array[@]' appears to be exactly the same as unset 'array'.
It completely eradicates the entire array, including the declaration
that it is associative.  Then when you do array[a/b]=c you are creating
a new integer-indexed array.  [a/b] becomes a math context, so it tries
to divide the value of a by the value of b (using 0 if either of those
is not set).

On that note, today I learned that you are not allowed to use either *
or @ as the index of an associative array in bash.  I guess I can see why,
but... that's probably going to break something some day.



reply via email to

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