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
set -u
In the beginning of your script, you could define your variables with an empty value
variable_undefined=""
Then
if [ "${variable_undefined}" == "" ]; then variable="$(...)" fi