Discussion:
[Help-bash] What keys are not allowed in an associative array?
Peng Yu
2018-06-09 23:03:31 UTC
Permalink
I see @ and * are not allowed. Is there anything else that is not allowed?

$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
declare -A x=([@]=1)
declare -A x=([*]=1)
declare -A x=([*a]=1)
$ ./main.sh
declare -A x=([@]=1)
./main.sh: line 5: [@]=1: invalid associative array key
declare -A x=([*]=1)
./main.sh: line 6: [*]=1: invalid associative array key
declare -A x=([*a]=1)
--
Regards,
Peng
João Eiras
2018-06-09 23:08:40 UTC
Permalink
Wrap your keys with quotes and the everything is allowed.
Greg Wooledge
2018-06-11 12:25:57 UTC
Permalink
The empty string is not allowed. As far as I know, that's the only one.

@ and * are allowed, if properly quoted.

wooledg:~$ declare -A aa
wooledg:~$ aa[""]=foo
bash-5.0: aa[""]: bad array subscript
wooledg:~$ aa["@"]=foo
wooledg:~$ declare -p aa
declare -A aa=(["@"]="foo" )

Loading...