Here is an example from https://unix.stackexchange.com/a/56846/674
A different, bash-specific way of testing whether a variable of any type has been declared is to check whether it's listed in
${!PREFIX*}
:case " ${!foobar*} " in *" foobar "*) echo "foobar is declared";; *) echo "foobar is not declared";; esac
If I am correct, to make a parameter not declared, we use
unset
builtin. Theunset
case works like the quote said,$ unset foobar $ case " ${!foobar*} " in > *" foobar "*) echo "foobar is declared";; > *) echo "foobar is not declared";; > esac foobar is not declared
but the
declare
d case doesn't:$ unset foobar $ declare foobar $ case " ${!foobar*} " in > *" foobar "*) echo "foobar is declared";; > *) echo "foobar is not declared";; > esac foobar is not declared
Is this a correct way to test if a parameter is declared?
- Also in
" ${!foobar*} "
, why are there spaces before and after the parameter expansion? Does it work whenfoobar
is an array or dictinary and when it is a variable but not an array or dictionary?