This is a topic I originally mentioned in another discussion on the new treatment of assoc_expand_once ( https://lists.gnu.org/archive/html/bug-bash/2021-03/msg00059.html ). As in the mail subject, I suggest supporting empty keys for associative arrays. Does anyone have any opinion on this suggestion?
Description: Currently, associative arrays do not accept empty keys. We can work it around e.g. by always prefixing `x' before the keys, such as `assoc[x$key]=$value'. But I still think we may support empty keys for associative arrays because I don't see strong reasons to disallow the empty keys of associative arrays. Here is the quote of the original discussion: 2021年3月16日(火) 10:41 Koichi Murase <myoga.mur...@gmail.com>: > Maybe this is unrelated to the extra expansions of array subscripts, > but is there any reason that Bash disallows empty associative keys? > This is also a point that confuses users related to the keys of > associative arrays. Associative arrays in other languages usually > accept an empty key. I usually use `a[x$key]' for the workaround, but > I personally feel we should support empty associative keys in Bash. I > also remember that at least two other people were confused by the > current behavior not accepting the empty key. Repeat-By: Here I summarize the current behavior for the empty keys. It seems Bash already parses empty keys for assignments, but they seem to be afterward rejected: $ declare -A a=() $ a[]=1 bash-dev: a[]: bad array subscript $ a['']=1 bash-dev: a['']: bad array subscript `a[]' in parameter expansions are not parsed. The variant `a['']' pass through the parsing phase but is later rejected: $ echo ${a[]} bash-dev: ${a[]}: bad substitution $ echo ${a['']} bash-dev: a: bad array subscript The variable name `a[]' is rejected in `printf -v', indirect expansions, and namerefs: $ printf -v 'a[]' 1 bash-dev: printf: `a[]': not a valid identifier $ printf -v 'a[""]' 1 bash-dev: a[""]: bad array subscript $ iref='a[]'; echo ${!iref} bash-dev: a[]: invalid variable name $ iref='a[""]'; echo ${!iref} bash-dev: a: bad array subscript $ declare -n nref='a[]'; echo $nref bash-dev: declare: `a[]': invalid variable name for name reference $ declare -n nref='a[""]'; echo $nref bash-dev: a: bad array subscript No error message for `unset 'a[]''? $ unset 'a[]' $ unset 'a[""]' bash-dev: unset: [""]: bad array subscript -- Koichi