As https://stackoverflow.com/a/13864829/ said,
$ if [ -z ${aaa+x} ]; then echo "aaa is unset"; else echo "aaa is set"; fi
aaa is unset
can test if a variable aaa
is set or unset.
How can I wrap the checking into a function? In bash, the following nested parameter expansion doesn't work:
$ function f() { if [ -z ${$1+x} ]; then echo "$1 is unset"; else echo "$1 is set"; fi };
$ f aaa
bash: ${$1+x}: bad substitution
Thanks.
bash
shell. Wrapping it in a function would serve no benefit. – Kusalananda Nov 14 '18 at 19:07