There is no difference between environment variables and variables in a script. Environment variables are just defined earlier, outside the script, before the script is called. From the script's point of view, a variable is a variable.
You can check if a variable is defined:
if [ -z "$a" ]
then
echo "not defined"
else
echo "defined"
fi
and then set a default value for undefined variables or do something else.
The -z
checks for a zero-length (i.e. empty) string. See man bash
and look for the CONDITIONAL EXPRESSIONS section.
You can also use set -u
at the beginning of your script to make it fail once it encounters an undefined variable, if you want to avoid having an undefined variable breaking things in creative ways.