[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: test_v doesn't work for associative arrays
From: |
Chet Ramey |
Subject: |
Re: test_v doesn't work for associative arrays |
Date: |
Fri, 10 Jun 2016 11:39:13 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 |
On 6/10/16 8:53 AM, pskocik@gmail.com wrote:
> Bash Version: 4.3
> Patch Level: 11
> Release Status: release
>
> Description:
> [Detailed description of the problem, suggestion, or complaint.]
>
> Repeat-By:
> #!/bin/bash
> declare var
> declare -a ary
> declare -A asoc
>
> var=1
> ary=(1)
> asoc[a]=1
>
> for a in var ary asoc; do
> printf '%s\t' "$a"
> if test -v "$a"; then
> echo ✓
> else
> echo ✗
> fi
> done
>
> <<OUTPUTS
> var ✓
> ary ✓
> asoc ✗
> OUTPUTS
Referencing an array variable without a subscript is equivalent to
referencing element 0 (or "0" for associative arrays). Plus, `test -v'
works on variable names, not dereferenced variables or array elements.
You can test whether or not a specific element is set by testing
whether it is non-empty (test -v 'ary[1]'), or test whether an array has
any elements set using something like [ ${#ary[@]} -gt 0 ].
One of the things that may come into a future version is something like
test -v 'array[@]' to see whether or not an array has any elements set.
Chet
--
``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/