可以将输入读取到关联数组的元素中吗?

Can one read input into a element of a associative array?

我想使用read将数据放入关联数组的特定元素中,所以我写了以下内容:

typeset -Ag aa
aa[key]='initial value'
...
read aa[key]

产生错误:no matches found: aa[key]

所以我改为编写以下内容并且有效:

typeset -Ag aa
aa[key]='initial value'
...
local line
read line
aa[key]=line

有没有办法跳过临时变量line

zsh 似乎试图以某种方式为 aa[key] 令牌进行通配。

因此,使用 noglob 或引用该标记暂时关闭 globbing 会很好。

echo val0 | noglob read aa[key]; echo $aa[key] ;# => val0

echo val1 | read 'aa[key]'; echo $aa[key] ;# => val1

noglob

Filename generation (globbing) is not performed on any of the words.

-- zshmisc(1) Precommand modifires