Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15716

Re: Two states of empty arrays

From Clint Hepner <clint.hepner@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Two states of empty arrays
Date 2019-12-12 14:06 -0500
Message-ID <mailman.739.1576177562.1979.bug-bash@gnu.org> (permalink)
References <c9224e23-ff7d-ba84-dc4f-aa68db902f72@noiraude.net> <CAKjp4B5tWjn-ttRuBu88EvpxuNFZo08PXouNODyJk=gwdyFD1w@mail.gmail.com>

Show all headers | View raw


On Thu, Dec 12, 2019 at 1:10 PM Léa Gris <lea.gris@noiraude.net> wrote:

> Hello,
>
> Depending on how an empty array is declared, it is not stored with the
> same state.
>
> # Empty array declared without parenthesis
> unset myArr
> declare -a myArr
> typeset -p myArr
> echo "${#myArr[@]}"
>
> output:
> declare -a myArr
> 0
>

Here, you haven't yet defined a parameter named myArr; you have only set
the array
attribute on the name myArr. You can see something similar with other
attributes:

$ declare -i x
$ [[ -v x ]] || echo "x not defined"
x not defined
$ declare -p x
declare -i x


>
> # Empty array declared without parenthesis
> unset myArr
> declare -a myArr=()
> typeset -p myArr
> echo "${#myArr[@]}"
>
> output:
> declare -a myArr=()
> 0
>
>
With the assignment, you have an actual parameter named myArr. Continuing
the integer attribute
example from above,

$ x=3
$ declare -p x
declare -i x="3"
$ [[ -v x ]] || echo "x not defined"   # No output


--
Clint

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Two states of empty arrays Clint Hepner <clint.hepner@gmail.com> - 2019-12-12 14:06 -0500

csiph-web