I'm trying out the associative arrays in bash 5.1.16 by loading a file into a hash table like so but it doesn't work if the file values are in quotes it returns null
:
#!/bin/bash -e
declare -A dict
while read key; do
dict[$key]=$key
done < test2.txt
echo "This doesn't work ${dict[a]} ${dict[b]}"
test2.txt file being:
"a"
"b"
"c"
But if I remove the quotes in test2.txt
it works fine. But what I can't understand is if I assign them manually with and without quotes both ways work.
#!/bin/bash -e
declare -A dict
dict[f]="f"
dict["g"]="g"
echo "This works ${dict["f"]} ${dict["g"]}"
My understanding from other SO answers is that the keys shouldn't be quoted but as you see in the previous example they work with quotes. So how can I load the file if the keys have quotes or the should have them removed before loading?