[Top][All Lists]

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

Re: certain strings both legal and illegal as associative array keys

From: Chet Ramey
Subject: Re: certain strings both legal and illegal as associative array keys
Date: Thu, 05 Mar 2015 18:14:13 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

On 3/1/15 1:05 AM, address@hidden wrote:

> Bash Version: 4.2
> Patch Level: 45
> Release Status: release
> Description:
> A string is either legal or not legal as a key for an associative array.  
> However, bash accepts certain keys in some contexts but not in other 
> contexts, making a few strings both legal and illegal as associative array 
> key values.  Bash's behavior should be consistent.
> For instance, consider a key containing a single-quote character, "a'b".  The 
> follow script allows this key to be both defined and accessed, but not 
> removed.  If it can't be removed, it should fail to be set in the first 
> place.  (Or, better, since it can be set and accessed, it should be able to 
> be unset.)
> The documentation says only that "associative arrays are referenced using 
> arbitrary strings."  If there are restrictions on the content of these 
> strings, that should be documented.
> #!/bin/bash
> declare -A foo
> foo[a]="one"
> foo["a'b"]="two"
> echo "address@hidden"
> echo ${foo[a]}
> echo ${foo["a'b"]}
> unset foo[a]
> unset foo["a'b"]

There are a couple of issues.  First, these two array subscripts, while
they appear identical, are not exactly the same:

unset foo["a'b"]

The first does not undergo any word expansions before the array
assignment code runs, so that code performs the appropriate word
expansions (everything except word splitting and filename generation).

The second undergoes the usual set of word expansions performed on all
command arguments, so the array code sees foo[a'b].

Right now, the two operations perform the same set of expansions on
the subscript, regardless of whether or not word expansion has already
been performed.  This results in an unmatched quote error from the
argument to `unset'.

At some point, I may take a look at changing this, but it would not be
backwards compatible, and that is undesirable.  It doesn't help you
now, either.

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

reply via email to

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