bash can't distinguish between empty and unset arrays
From:
Martijn Dekker
Subject:
bash can't distinguish between empty and unset arrays
Date:
Wed, 03 Feb 2016 23:43:37 +0000
User-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
bash treats an empty array as if it were an unset variable, which seems
very illogical as empty is quite distinct from unset:
$ myarray=()
$ [[ -v myarray ]] && echo set || echo unset
unset
$ set | grep ^myarray= # yet, it's set:
myarray=()
$ set -u
$ for i in "${x[@]}"; do :; done
bash: x[@]: unbound variable
Note also that the "unbound variable" error is inconsistent with the
behaviour of "$@"; I would have thought that, logically, "$@" and
"${x[@]}" should behave the same way, since arrays are implemented as a
logical extension of the positional parameters concept.
zsh and ksh93 can distinguish between empty and unset arrays.
Thanks,
- Martijn