Re: [Help-bash] Why bash does not recoganize array type smartly?
From:
Dan Douglas
Subject:
Re: [Help-bash] Why bash does not recoganize array type smartly?
Date:
Tue, 15 Mar 2016 10:49:07 +0000
On Thu, Mar 10, 2016 at 4:16 AM, Peng Yu <address@hidden> wrote:
> Is there any rationale why bash is defined in this way. Wouldn't it
> better to allow users define associative array just by using something
> like `x=([a]=1 [b]=2)` without having to declare it?
That's how it works in ksh93, but that's the only exception. In ksh93
an initializer list that specifies indicies for sub-assignments is
implicitly an associative array of strings until given another type. A
compound assignment that's just a space-separated list of words is
implicitly an arithmetic indexed array of strings.
Everywhere else, it's always an indexed array unless you give it a type.
bash/ksh93:
typeset -A arr=([foo]=bar) # Awesome
bash/ksh93/zsh:
typeset -A arr; arr[foo]=bar arr[bar]=baz # Nasty but compatible
mksh has the problems of zsh in reverse -- it requires the assignment
and the typeset to be separate commands (like zsh), and doesn't
support associative arrays at all (unlike zsh), but at least it does
support arr=([key]=value) syntax (also unlike zsh).