[Top][All Lists]

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

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

From: Chet Ramey
Subject: Re: test '-v' - associative vs. normal array discrepancy - a bug ?
Date: Wed, 19 Nov 2014 14:24:43 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:24.0) Gecko/20100101 Thunderbird/24.6.0

On 11/19/14, 11:20 AM, Jason Vas Dias wrote:
> Good day -
> Please could anyone explain why the first command below produces no output:
> $ ( declare -A a=([a]=1);   if [ -v a ]; then echo yes; fi )
> $ ( declare -a  a=([0]=1);  if [ -v a ]; then echo yes; fi )
> yes
> $

Dereferencing an array without a subscript is equivalent to referencing
element 0 (integer or string, for indexed and associated arrays,
respectively).  Had you assigned the value to subscript 1 in the indexed
array, you wouldn't have gotten `yes' either.

If you want to test whether any element in an associative array is set,
you can do that:

if [ ${#a[@]} -gt 0 ]; then echo yes; fi

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/

reply via email to

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