bug-bash
[Top][All Lists]
Advanced

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

Re: Two states of empty arrays


From: Clint Hepner
Subject: Re: Two states of empty arrays
Date: Thu, 12 Dec 2019 14:06:06 -0500

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


reply via email to

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