[Top][All Lists]

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

Issue with 'key' for associative arrays

From: Frans de Boer
Subject: Issue with 'key' for associative arrays
Date: Wed, 25 Nov 2020 14:48:44 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0


Unless I have not read the manual properly, bash has an issue with the key part used to address a value within associated arrays. I tried this in the bash 4.4.x and 5.x series of bash under OpenSUSE 15.2 and TW.

Addressing associative arrays with an array statement - like echo ${k["${a[x]}"] does not work. The resulting data from ${a[x]} is technical a string, but is not treated as such when using  an associative array. Instead, one should first copy the contents of the indexed ${a[x]} array  to an ordinary shell variable and then use this variable as the key in an associative array.

Next a script to clarify my point:
! /bin/bash

declare -A  k
declare -a  a
declare -i  i=2
declare     sString="devel packager's guide"


echo ${k["$sString"]}
echo ${a[$i]}

unset -v 'k["${a[$i]}"]' # does not work
echo ${k["$sString"]}

unset -v 'k["$sTemp"]'   # does work
echo ${k["$sString"]}

unset -v a[$i]

Is this an intended feature or a possible bug?

--- Frans.

A: Yes, just like that                            A: Ja, net zo
Q: Oh, Just like reading a book backwards         Q: Oh, net als een boek 
achterstevoren lezen
A: Because it upsets the natural flow of a story  A: Omdat het de natuurlijke 
gang uit het verhaal haalt
Q: Why is top-posting annoying?                   Q: Waarom is Top-posting zo 

reply via email to

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