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
unsetbuiltin. Theunsetcase works like the quote said,$ unset foobar $ case " ${!foobar*} " in > *" foobar "*) echo "foobar is declared";; > *) echo "foobar is not declared";; > esac foobar is not declaredbut the
declared case doesn't:$ unset foobar $ declare foobar $ case " ${!foobar*} " in > *" foobar "*) echo "foobar is declared";; > *) echo "foobar is not declared";; > esac foobar is not declaredIs 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 whenfoobaris an array or dictinary and when it is a variable but not an array or dictionary?