I just discovered set -u in bash and it helped me find several previously unseen bugs. But I also have a scenario where I need to test if a variable is defined
In bash 4.2 and newer there is an explicit way to check whether a variable is set, which is to use -v. The example from the question could then be implemented like this:
if [[ ! -v variable ]]; then
variable="$(...)"
fi
See http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions
If you only want to set the variable, if it is not already set you are probably better of doing something along these lines:
variable="${variable-$(...)}"
Note that this does not deal with a defined but empty variable.