Parker Coates Parker Coates - 5 years ago 342
Bash Question

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

I want to use

read
to put data into a particular element of an associative array, so I wrote the following:

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


which produces an error:
no matches found: aa[key]


So I write the following instead and it works:

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


Is there a way to skip the temporary variable
line
?

Answer Source

It seems that zsh trys to do globbing somehow for the aa[key] token.

So, it would be fine to turn off globbing temporally using noglob or quoting that token.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download