bash associative array key string with colon is giving error

十年热恋 提交于 2019-12-20 04:59:10

问题


I am creating an associative array of source and destination MAC addresses.

$ 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")

How can I tell bash that the given key is a whole string.


回答1:


That's not an associative array. You need to use declare -A, not 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 creates a numerically indexed array, so the shell is trying to parse your index as a number.



来源:https://stackoverflow.com/questions/40406187/bash-associative-array-key-string-with-colon-is-giving-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!