[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Awkward behavior of empty arrays
From: |
Chet Ramey |
Subject: |
Re: [Help-bash] Awkward behavior of empty arrays |
Date: |
Thu, 31 Aug 2017 10:34:42 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 |
On 8/31/17 4:38 AM, Cristian Zoicas wrote:
>
> Hello all
>
> I want to create arrays and use them (easily). I am especially
> interested in empty arrays and I felt very uncomfortable with them
> since I found some counterintuitive or undocumented behaviors. I
> provide some examples below.
>
> Example 1:
>
> 1 set -u;
> 2 unset A;
> 3 declare -a A=;
> 4 echo "s1";
> 5 echo "size: address@hidden";
> 6 echo "s2";
> 7 for e in address@hidden; do echo "e: ${e}"; done
>
> Starting with the line 3 I want to create an empty array, so I
> declare A to have the array attribute and then I (hope to) assign
> null to it. Some people may say that I am not right because the
> manual says "An array variable is considered set if a subscript
> has been assigned a value." They are somehow right because I did
> not assign any value to any subscript, so the array should not be
> initialized. However let's see what is going on.
>
> Even if I did not assign any subscript a value, the line 5 says
> that the size of the array is 1 and it is possible to reference
> element ${A[0]} without receiving any error due to "set -u". Also
> address@hidden expands to nothing.
>
> If the manual is right, then the array should not be defined. It
> should not exist and I should receive errors due to 'set -u', but
> even 'declare -p A' shows that the array exists and it has a
> single element whose value is null.
>
> Is this behavior of assigning null to an array an to have an array
> with a single null element the desired behavior?
Yes. The manual also says that assigning to an array without using a
subscript is equivalent to assigning to element 0 (or "0").
The other significant consequence of this is treating unsubscripted
references to an array as references to element 0 (or "0").
>
> Example 2:
>
> 1 set -u;
> 2 unset A;
> 3 declare -a A=();
> 4 echo "s1";
> 5 echo "size: address@hidden";
> 6 echo "s2";
> 7 for e in address@hidden; do echo "e: $e"; done
>
> Starting with the line 3 I want to create an empty array, so I
> declare A to have the array attribute and then I (hope to) assign
> an empty array to it.
>
> The line 5 behaves well and says that the size of the array is 0,
> thus I imagine that the array is empty. So according to the manual
> address@hidden should expand to nothing and the for loop in the line 7
> should not print anything. Instead the message 'bash: address@hidden:
> unbound
> variable' is printed. As if A is not set.
It isn't set because there aren't any subscripts with values. Ultimately,
`declare -a A' and `declare -a A=()' are equivalent.
The real question is whether or not the size/length operator should error
out when there are no elements in the array. The current behavior could be
considered a bug.
>
> There is nothing in the manual that says that the () cannot be used
> to create empty arrays. The syntax is also accepted
>
> It means that if the size of the array is 0 then the array cannot
> be expanded. Otherwise it can. Such behavior forces people to
> write difficult to understand/maintain code.
>
> Example 3:
>
> 1 set -u;
> 2 unset A;
> 3 declare -a A=(1); unset A[0];
> 4 echo "s1";
> 5 echo "size: address@hidden";
> 6 echo "s2";
> 7 for e in address@hidden; do echo "e: $e"; done
>
> The behavior of this example is similar with the behavior of the
> example 2 above.
There still aren't any subscripts with values.
Now, "making sense" is in the eye of the beholder. I could see an
array with no elements being `set' in the sense that a string variable
whose value is null being set. I did not choose that alternative.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/
- Re: [Help-bash] Awkward behavior of empty arrays, (continued)
- Re: [Help-bash] Awkward behavior of empty arrays, Greg Wooledge, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Chet Ramey, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Andy Chu, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, DJ Mills, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Andy Chu, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Greg Wooledge, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Chet Ramey, 2017/08/31
Re: [Help-bash] Awkward behavior of empty arrays,
Chet Ramey <=
Re: [Help-bash] Awkward behavior of empty arrays, Chris F.A. Johnson, 2017/08/31