1. Overview
As Linux users, we frequently work with shell scripts. One of the common requirements in scripts is to test if a particular environment variable is set or not. This helps in handling various error conditions.
In this tutorial, we’ll see the various example to check if a particular environment variable is set or not.
2. Using the if Conditional Expression
We can use the if conditional expression to check if a value is assigned to a variable or not:
$ VAR=
$ if [ x"${VAR}" == "x" ]; then
echo "Value is not assigned to a variable"
else
echo "Value is assigned to a variable"
fi
Value is not assigned to a variable
Let’s assign a value to a variable and verify the result:
$ VAR="sample-value"
$ if [ x"${VAR}" == "x" ]; then
echo "Value is not assigned to a variable"
else
echo "Value is assigned to a variable"
fi
Value is assigned to a variable
3. Using the Double Square Brackets
Another way to check if a variable is set or not is by using a double square bracket:
$ VAR=
$ [[ x"${VAR}" == "x" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable
Let’s verify the output by assigning a value to a variable:
$ VAR="sample-value"
$ [[ x"${VAR}" == "x" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable
Note that this method works only with Bash, Z Shell (zsh), and Korn Shell (ksh).
4. Using the Parameter Expression
We can use bash built-in parameter expressions to check if a variable is set or not:
$ VAR=
$ [[ ${VAR:-"unset"} == "unset" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable
Let’s set the value of a variable and check whether it works:
$ VAR="sample-value"
$ [[ ${VAR:-"unset"} == "unset" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable
5. Using the -z Conditional Expression
In bash, there is a -z conditional expression that returns true if the length of a string is zero. We can use this property to check if a variable is set or not:
$ VAR=
$ [[ -z "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable
Let’s try it out:
$ VAR="sample-value"
$ [[ -z "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable
6. Using the -n Conditional Expression
Similarly, there is -n conditional expression that returns true if the length of a string is non-zero. We can use this property to check if a variable is set or not:
$ VAR=
$ [[ ! -n "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable
Let’s update the value of a variable and confirm the result:
$ VAR="sample-value"
$ [[ ! -n "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable
7. Conclusion
In this tutorial, we discussed various practical examples to check if a variable is set or not. First, we discussed if conditional expression. Then we discussed parameter expression, and finally, we discussed built-in conditional expressions of the bash. We can use these commands in day-to-day life to make shell scripts more robust.