Sometimes you might want to exit a command if a variable is not set in Bash.

You can do this as follows:

if [ -z "$VARIABLE_NAME" ]; then echo "VARIABLE_NAME is not set"; exit 1; fi

I add this at the top of deployment scripts to catch missing environment variables early — better to fail immediately with a clear message than to have the script run halfway and leave things in a broken state. The -z flag checks if the string is empty, which covers both unset and empty-string cases.

For background, see Linux.

An alternative is set -u (or set -o nounset) at the top of your script, which makes Bash exit on any unset variable reference automatically. That’s a broader safety net but can be too aggressive if you intentionally use optional variables.