TIL: how to check if a bash env variable is set - jrott

TIL: how to check if a bash env variable is set

To check that an environment variable is set in bash and stop the script if it isn’t you can use the -z flag to check it’s length. Unassigned variables are treated as having a length of zero.

set -e 

if [[ -z "$FOO" ]]; then
    echo "FOO is unassigned or an empty string"s
    exit 1
else
    echo "FOO has the value: $FOO"
fi