The closest solution I came to is:
declare -A foobar=([foo]=bar [bar]=foo)
(set -u; true ${foobar[foo]}) 2>/dev/null
Ideally I would like to use test -v to test if an array key is defined. But test -v foobar[bar] apparently always returns 1. Also I don’t want a global set +u and run the risk to access an undefined variable.
As of bash version “4.2.39(1)-release” I cannot see how to test for the presence of an array key with the
test -vcommand.test -v foobar[foo]simply always fails. To me the best option looks like one should test with the${foobar[foo]-bar}parameter substitution. In order to re-test Bash’s behavior with shopt’snounsetshell option and every new release I wrote the following Bash shell script: