0

Bash manual says:

Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0.

In Bash or POSIX shell, is there some general rule for when an array variable represents the entire array, and when it represents the element indexed by 0?

For example,

  1. It seems that after declare an array variable means the entire array, while in parameter expansion, it means the element indexed by 0:

    $ declare -p x
    declare -a x='([1]="b" [2]="c")'
    $ echo $x
    
  2. In [[ -v myarr ]], does myarr mean the array or myarr[0]?
1
3

I think your confusion is on the word "referencing."

You're not referencing the array variable after declare at all; you're only giving the name x.

To reference a variable you have to put a $ before the name.

Try searching through the bash man page for all instances of referenc and read them.


As for the second question:

In [[ -v myarr ]], does myarr mean the array or myarr[0]?

By test, it means myarr[0].

$ declare -p myarr
bash: declare: myarr: not found
$ myarr=()
$ declare -p myarr
declare -a myarr='()'
$ echo $myarr

$ [[ -v myarr ]] && echo is set
$ [[ -v myarr[0] ]] && echo is set
$ [[ -v myarr[1] ]] && echo is set
$ myarr+=([1]=b)
$ declare -p myarr
declare -a myarr='([1]="b")'
$ echo $myarr

$ [[ -v myarr ]] && echo is set
$ [[ -v myarr[0] ]] && echo is set
$ [[ -v myarr[1] ]] && echo is set
is set
$ myarr+=([0]=q)
$ declare -p myarr
declare -a myarr='([0]="q" [1]="b")'
$ echo $myarr
q
$ [[ -v myarr ]] && echo is set
is set
$ [[ -v myarr[0] ]] && echo is set
is set
$ [[ -v myarr[1] ]] && echo is set
is set
$ 
13
  • thanks. In [[ -v myarr ]], does myarr mean the array or myarr[0]?
    – Tim
    May 4 '16 at 0:55
  • @Tim, there's no such test as -v that I can find. It throws a syntax error on my version of bash.
    – Wildcard
    May 4 '16 at 0:58
  • add a space before ]]
    – Tim
    May 4 '16 at 0:59
  • 1
    So In [[ -v myarr ]], myarr means myarr[0], without $ in front of myarr?
    – Tim
    May 4 '16 at 7:46
  • 1
    @Wildcard And yes, you are correct, Sir. I tested in the wrong version of bash. It works in bash 4.3 and 4.4. However, in versions 4.3 and 4.4, the correct test for the whole array is [[ -v myarr[@] ]]. Please try unset myarr; myarr[88]=''; [[ -v myarr[@] ]] && echo "is set" (also work with Associative arrays). Cheers.
    – user79743
    May 7 '16 at 4:58
0

You are mixing variables with strings that represent "variable names".

A $variable is the variable, variable is just an string that is interpreted as a variable by some commands.

Try this:

$ a=x
$ x=(111 222 333)
$ declare -p $a
declare -a x='([0]="111" [1]="222" [2]="333")'

The expansion of variable a ($a) gave string x, which was interpreted by declare as a "variable-name" and then expanded and printed.

In [[ -v myarr ]] also the test is interpreting the string myarr as a variable, therefore it is always the whole variable.

Try

$ a=myarr
$ unset myarr
$ [[ -v $a ]] && echo set || echo unset
unset

$ myarr[5]=five
$ [[ -v $a ]] && echo set || echo unset
set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.