[Top][All Lists]

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

test -v for array does not work as documented

From: Ulrich Mueller
Subject: test -v for array does not work as documented
Date: Wed, 19 Feb 2020 13:00:15 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.60 (gnu/linux)

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: x86_64-pc-linux-gnu-gcc
Compilation CFLAGS: -march=native -ggdb -O2 -pipe -Wno-parentheses 
uname output: Linux a1i15 4.19.102-gentoo #1 SMP Fri Feb 7 14:32:53 CET 2020 
x86_64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz GenuineIntel GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.0
Patch Level: 16
Release Status: release

        The GNU Bash Reference Manual Version 5.0 says about test -v:
        '-v VARNAME'
             True if the shell variable VARNAME is set (has been
             assigned a value).

        However, it doesn't always return true when VARNAME is an
        (indexed or associative) array that has been assigned a value.

        See also Gentoo bug https://bugs.gentoo.org/710076 for some
        context and additional examples.

        $ var1=()
        $ var2[1]=a
        $ var3=( b c )
        $ declare -A var4; var4["foo"]=d
        $ declare -p var0 var1 var2 var3 var4
        bash: declare: var0: not found
        declare -a var1=()
        declare -a var2=([1]="a")
        declare -a var3=([0]="b" [1]="c")
        declare -A var4=([foo]="d" )
        $ [[ -v var0 ]]; echo $?
        $ [[ -v var1 ]]; echo $?
        $ [[ -v var2 ]]; echo $?
        $ [[ -v var3 ]]; echo $?
        $ [[ -v var4 ]]; echo $?

        I would expect 1 only for var0, but 0 for all the others (which
        have been assigned a value).

        In fact, bash version 4.2.53 behaves as expected, whereas I get
        the above result with versions 4.4.23 and 5.0.16.

reply via email to

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