[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Why empty key is not allowed?
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] Why empty key is not allowed? |
Date: |
Sun, 22 Sep 2019 12:27:13 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-09-21 18:18:40 -0500, Peng Yu:
[...]
> I am trying to assign an empty key to an associated array.
>
> $ declare -A x
> $ x[]=1
> -bash: x[]: bad array subscript
>
> But I got this error. Is it not allowed? Or I am not doing it correctly?
>
> The manpage says this. So empty string should be allowed?
[...]
Yes, it's a known limitation of bash. ksh93 (whose API bash
copied for the most part), and zsh don't have the problem.
In ksh93, you need:
x[""]=1 (or x['']=1, or x[$empty]=1...)
In zsh neither x[]=1 nor x[""]=1 work (the latter sets an
element of key ""), but a+=('' 1) or a[$empty]=1 do work.
Note that of the 3, only in zsh can key and/or values contain
NUL characters.
--
Stephane