help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Awkward behavior of empty arrays


From: Cristian Zoicas
Subject: [Help-bash] Awkward behavior of empty arrays
Date: Thu, 31 Aug 2017 10:38:49 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40


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?

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.

   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.

I would like to know if these behaviour make sense.

regards
Cristian










reply via email to

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