带冒号的bash关联数组键字符串给出错误

时间:2016-11-03 16:01:59

标签: bash unix associative-array

我正在创建一个源和目标MAC地址的关联数组。

$ declare -a SrcDstMap
$ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
-bash: 9c: value too great for base (error token is "9c")
$ SrcDstMap["fc:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
-bash: fc:4e:20:73:e2:72: syntax error in expression (error token is ":4e:20:73:e2:72")

如何告诉bash给定的键是一个完整的字符串。

1 个答案:

答案 0 :(得分:4)

那不是关联数组。您需要使用declare -A,而不是declare -a

$ declare -A SrcDstMap
$ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff"
$ declare -p SrcDstMap
declare -A SrcDstMap=([9c:4e:20:73:e2:72]="ff:ff:ff:ff:ff:ff" )

declare -a创建一个数字索引数组,因此shell试图将索引解析为数字。