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: Greg Wooledge
Subject: Re: [Help-bash] Awkward behavior of empty arrays
Date: Thu, 7 Sep 2017 08:18:48 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Thu, Sep 07, 2017 at 11:31:01AM +0200, Cristian Zoicas wrote:
> > Hm, interesting.  I think your whole message below can be rephrased as: 
> > empty arrays don't work with set -u.  Is that right?
> 
> right...

You know, I'd been taking this assertion on faith, but after testing
it, I just don't see any evidence that it's true.

wooledg:~$ set -u
wooledg:~$ unset a
wooledg:~$ for i in "address@hidden"; do :; done
wooledg:~$ a=()
wooledg:~$ for i in "address@hidden"; do :; done
wooledg:~$ a="a string"
wooledg:~$ for i in "address@hidden"; do :; done
wooledg:~$ unset a
wooledg:~$ a=([1]=foo)
wooledg:~$ for i in "address@hidden"; do :; done
wooledg:~$ echo "${a[0]}"
bash: a[0]: unbound variable
wooledg:~$ set +u

The ONLY way I can get -u to trigger is to reference a specific index
that is not set within the array.  In all other cases, an array that
is either unset or empty, or which has an index gap, works just
fine with "address@hidden" under -u.

Of course, -u is still horrible and I will never use it my scripts.
But it's not because of how it treats arrays.



reply via email to

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