[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Does declaring an array variable initialize it?
From: |
Chet Ramey |
Subject: |
Re: Does declaring an array variable initialize it? |
Date: |
Thu, 20 Mar 2014 07:27:32 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 |
On 3/20/14 12:41 AM, Richard Tollerton wrote:
>
> Old behavior, bash 4.2.45:
>
> $ bash -c 'set -u; echo address@hidden'; \
> bash -c 'set -u; declare var1; echo ${#var1}'; \
> bash -c 'set -u; declare -a arr2; echo address@hidden'
> bash: arr1: unbound variable
> bash: var1: unbound variable
> 0
>
> New behavior, bash 4.3:
>
> $ bash -c 'set -u; echo address@hidden'; \
> bash -c 'set -u; declare var1; echo ${#var1}'; \
> bash -c 'set -u; declare -a arr2; echo address@hidden'
> bash: arr1: unbound variable
> bash: var1: unbound variable
> bash: arr2: unbound variable
>
> I suppose that this change in behavior makes array variables more
> consistent with normal variables, but I couldn't find anything in
> CHANGES which obviously relates to this, so I'm not sure if this is a
> bug or not.
There is this, in the list of changes in bash-4.3-beta:
f. Fixed several cases where `invisible' variables (variables with attributes
but no values, which are technically unset) were treated incorrectly.
> Was I always mistaken in figuring that declaring an array also
> initialized it?
A variable is not set until it has been assigned a value. This was always
true, and always supposed to apply to array variables as it did to scalars.
bash-4.2 had a number of inconsistencies here, and I fixed some number of
those. I'm sure some still remain.
This topic has come up several times in the past, most recently a couple
of weeks ago:
http://lists.gnu.org/archive/html/bug-bash/2013-01/msg00068.html
http://lists.gnu.org/archive/html/bug-bash/2013-11/msg00000.html
http://lists.gnu.org/archive/html/bug-bash/2014-03/msg00052.html
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/