help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Awkward behavior of empty arrays


From: Andy Chu
Subject: Re: [Help-bash] Awkward behavior of empty arrays
Date: Thu, 31 Aug 2017 09:03:01 -0700

On Thu, Aug 31, 2017 at 5:14 AM, Greg Wooledge <address@hidden> wrote:

> On Thu, Aug 31, 2017 at 02:51:58AM -0700, Andy Chu wrote:
> > also invalid:
> > "${A}"  # this is an implicit "address@hidden" but is confusing
>
> Actually, it's even worse than that.  "${A}" and "$A" and "${A[0]}"
> are all equivalent.
>
> wooledg:~$ A=(an array); echo "${A}"
> an
> wooledg:~$ unset A; A[1]=an; echo "${A}"
>
> wooledg:~$ unset A; declare -A A; A[zebra]=an A[1]=ugly A[0]=array; echo
> "${A}"
> array
>
> The equivalence of $A == ${A[0]} is one of the most horribly annoying
> misfeatures ever.
>

Yes sorry, that was a typo.  I meant implicit "${A[0]}" not implicit
"address@hidden".

In the same way that you want to avoid

declare -a A=x

in favor of:

declare -a A=(x)

You also want to avoid "$A" in favor of "{$A[0]}".  In both cases there is
a confusion between A and A[0] (considering arrays as you do in any other
programming language.)

Andy


reply via email to

[Prev in Thread] Current Thread [Next in Thread]