我面对一种非常奇怪的行为。
#!/bin/bash
declare -A array_first=(
[name]="Array 1"
[message]="Hi there"
)
declare -A array_second=(
[name]="Array 2"
[message]="Bye!"
)
arrays=(array_first array_second)
for arr in ${!arrays[*]}
do
val=${arrays[$arr]}
val=${!val}
echo ${val[name]} # Why this string is empty?
done
echo "${array_first[name]}"
echo "${array_first[message]}"
echo "${array_second[name]}"
echo "${array_second[message]}"
我需要关联数组的值,并显示一个空字符串。我做错了什么?
答案 0 :(得分:1)
将${!...}
与数组合并时,您必须在使用!
评估的字符串中指定索引或键:
for arr in ${!arrays[*]}
do
val=${arrays[$arr]}
ex="$val[name]"
echo ${!ex} # No longer empty.
done