Environment variables are a fundamental concept in shell scripting, allowing you to store and retrieve values that can be used throughout your script. In this tutorial, we will explore how to check if an environment variable exists and retrieve its value.
Introduction to Environment Variables
An environment variable is a named value that is stored outside of a script and can be accessed from within the script. Environment variables are typically set before running a script using the export
command. For example:
export DEPLOY_ENV=dev
This sets an environment variable named DEPLOY_ENV
with the value dev
.
Checking if an Environment Variable Exists
To check if an environment variable exists, you can use the -z
test, which checks if a string is empty. For example:
if [ -z "${DEPLOY_ENV}" ]; then
echo "Environment variable DEPLOY_ENV does not exist"
else
echo "Environment variable DEPLOY_ENV exists with value ${DEPLOY_ENV}"
fi
This code checks if the DEPLOY_ENV
environment variable is empty. If it is, the script prints a message indicating that the variable does not exist.
Retrieving the Value of an Environment Variable
To retrieve the value of an environment variable, you can simply use the variable name in your script. For example:
MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
This sets the MY_SCRIPT_VARIABLE
variable to the value of the DEPLOY_ENV
environment variable.
Setting a Default Value for an Environment Variable
If an environment variable does not exist, you can set a default value using parameter expansion. For example:
MY_SCRIPT_VARIABLE="${DEPLOY_ENV:-default_value}"
This sets the MY_SCRIPT_VARIABLE
variable to the value of the DEPLOY_ENV
environment variable if it exists, or to the string default_value
if it does not.
Using Parameter Expansion
Parameter expansion is a powerful feature in shell scripting that allows you to perform various operations on variables. The syntax for parameter expansion is ${parameter:-word}
, where parameter
is the name of the variable and word
is the default value to use if the variable is empty or unset.
For example:
var=${DEPLOY_ENV:-default_value}
This sets the var
variable to the value of the DEPLOY_ENV
environment variable if it exists, or to the string default_value
if it does not.
Differences between -
and :
When using parameter expansion, you can use either the -
or :
character to specify the default value. The difference between the two is that -
checks if the variable is empty or unset, while :
only checks if the variable is unset.
For example:
unset DEPLOY_ENV
echo "${DEPLOY_ENV:-default_value}" # prints "default_value"
echo "${DEPLOY_ENV-default_value}" # prints "default_value"
DEPLOY_ENV=""
echo "${DEPLOY_ENV:-default_value}" # prints "default_value"
echo "${DEPLOY_ENV-default_value}" # prints ""
As you can see, using -
will print the default value if the variable is empty or unset, while using :
will only print the default value if the variable is unset.
Best Practices
When working with environment variables in shell scripts, it’s a good idea to follow these best practices:
- Always check if an environment variable exists before trying to use its value.
- Use parameter expansion to set default values for environment variables that may not exist.
- Be aware of the differences between
-
and:
when using parameter expansion.
By following these best practices, you can write robust and reliable shell scripts that work with environment variables.