[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: No expansions performed while declaring an associative array using a
From: |
Chet Ramey |
Subject: |
Re: No expansions performed while declaring an associative array using a list of keys and values |
Date: |
Fri, 11 Dec 2020 11:41:34 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 |
On 12/11/20 7:08 AM, Oğuz wrote:
I was trying the new features of bash 5.1 and came across this inconsistent
behavior:
$ foo='1 2'
$ declare -A bar=($foo 3)
$ declare -p bar
declare -A bar=(["\$foo"]="3" )
$
$ bar+=($foo 3)
$ declare -p bar
declare -A bar=(["\$foo"]="3" ["1 2"]="3" )
Is there a particular reason to avoid performing expansions in `declare -A
bar=($foo 3)'?
The idea is that each word in the list undergoes the same expansion as the
keys and values in a [$key]=value word in the compound assignments. Right
now, the implementation is pretty simple. For instance, if you use
something like
set -- a b c d e f g
declare -A v
v=( "$@" )
you'll get an associative array with a single key 'a b c d e f g' and a
null value.
I agree that it would be useful to have
foo='1 2'
declare -A v1=$( $foo 3 )
declare -A v2=$( [$foo]=3 )
set the two arrays to the same contents, so I'll see what needs to change
to do that.
Chet
--
``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/
Re: No expansions performed while declaring an associative array using a list of keys and values, felix, 2020/12/14