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:27:08 -0700

On Thu, Aug 31, 2017 at 7:57 AM, Greg Wooledge <address@hidden> wrote:
>
>
> The only way to compare two arrays for equivalence correctly is to write
> a loop.


Yes, that was an unintentional omission from the rules:

"3. Don't do anything with arrays except COPY them and SPLICE them into
commands."

->

"3. Don't do anything with arrays except ITERATE over them, COPY them, or
SPLICE them into commands."

So

for i in "address@hidden"; do ... done

is valid but these are not:

for i in address@hidden; do ... done
for i in ${A[*]}; do ... done
for i in "${A[*]}"; do ... done

The point is that you can express any semantics you want with a restricted
set of syntactic rules.  The latter 3 can all be expressed using the
$joined pattern I gave.

If you want to see more type confusion, including between arrays and
associative arrays, look at:

https://github.com/oilshell/oil/blob/master/spec/type-compat.test.sh

This came from a discussion here:

https://github.com/oilshell/oil/issues/26

You can make a test matrix of all combinations:

declare -a    declare +a     declare -A
=(foo)       =''        =([key]=value)

and get a VERY VERY confusing set of semantics.  They are not orthogonal at
all, similar to the fact that $@ and $* are not orthogonal with double
quoting.

(Actually, the test matrix is even larger than that, because sometimes
array syntax is parsed inside quoted strings!  Also, there are two
different += operators -- one in the command language, and one in the arith
language.)

Here is an excerpt of my reasoning:

"""
The problem is that bash has two ways to express an array -- -a vs +a, and
(a b c) vs 'a b c'. I don't see why these two ways are necessary. In Oil I
just use the presence of array literals to tell me.

That is,

- declare -a myarray can simply be written declare myarray=().
- declare +a mystring can simply be written declare mystring=''

This doesn't work in bash but it has no ambiguity. It's a little more
Python-like, where objects have types,not variables.
"""

Andy


reply via email to

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