As Wumpus stated in the comments, the problem is that you've declared a regular, numerically-indexed array, when you clearly wanted an associative array. In the context of a numerically-indexed array, indices are arithmetic expressions, which can result in confusing errors, or no errors when you might expect an error!
$ declare -a foo
$ foo[abc-def]=bar
This is legal, but does not assign "bar" to the index "abc-def". It assigns "bar" to the index 0, which is what abc
, def
and abc-def
all expand to, since they are not assigned. In other words, you're subtracting 0 from 0.
$ echo "${foo[0]}"
bar
If you try to escape the dashes you get an error, like the one you saw.
$ echo $(( abc \- def ))
bash: abc \- def : syntax error: invalid arithmetic operator (error token is "\- def ")
But you can use an associative array here, instead:
$ declare -A bar
$ bar[abc-def]=xyzzy
$ echo "${bar[abc-def]}"
xyzzy
This allows you to use strings in array indices, and they do not resolve to arithmetic expressions.
Edit: bad array subscript
I didn't see the bad array subscript at first, because you only get that on the first assignment to the array.
$ unset foo
$ foo[-1]=bad
bash: foo[-1]: bad array subscript
$ foo[0]=whatevz
$ foo[-1]=bad
$ # no error!