Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash associative array key string with colon is giving error

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.

like image 910
Necktwi Avatar asked Sep 24 '26 19:09

Necktwi


1 Answers

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.

like image 57
Charles Duffy Avatar answered Sep 26 '26 13:09

Charles Duffy