help-bash
[Top][All Lists]
Advanced

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

Re: How to test if a variable exists and is a hash?


From: Marco Ippolito
Subject: Re: How to test if a variable exists and is a hash?
Date: Sat, 11 Jul 2020 16:08:40 -0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

On 11/07/2020 12:43, Peng Yu wrote:
$ declare -A x=([a]=10); [[ -v x ]]; echo $?
1

-v can not test if a hash is declared.

`declare -p x 2> /dev/null` can not tell whether it is a hash or a variable.

What is the best way to check if a variable exists and is a hash? Thanks.


By the way, maybe compgen could be enhanced to distinguish between
associative and non-associative arrays?

```
$ bash --noprofile --norc \
    -c 'a=(); declare -A b=([x]=y); compgen -A arrayvar'
BASH_ALIASES
BASH_ARGC
BASH_ARGV
BASH_CMDS
BASH_LINENO
BASH_SOURCE
BASH_VERSINFO
DIRSTACK
GROUPS
PIPESTATUS
a
b
```



reply via email to

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