In a bash script, I'm trying to test for the existence of a variable. But no matter what I do, my "if" test returns true. Here's the code:
ignored-deps-are-not-set () { if [ -z "${ignored_deps+x}" ] then return 0 fi return 1 } ignored_deps=1 ignored-deps-are-not-set echo "ignored-deps function returns: $?" if [ ignored-deps-are-not-set ] then echo "variable is unset" else echo "variable exists" fi Here is the output as written:
ignored-deps function returns: 1 variable is unset And the output when I comment out the line where ignored_deps is set.
ignored-deps function returns: 0 variable is unset No matter what, it says the variable is unset. What am I missing?
-is not a valid char in a variable name. How is this "ignored-deps-are-not-set" name allowed?/bin/shrejects the function name, but/bin/bashaccepts it, dashes and all. Since regular commands can have dashes in the name, it makes sense for bash to allow them too; it is likely not mandated by the POSIX standard, though.