Checking if a Variable is Set in Bash

In Bash, it’s often necessary to check if a variable has been set before attempting to use its value. This can be particularly important when writing scripts that accept user input or parameters. In this tutorial, we’ll explore the different ways to check if a variable is set in Bash.

Using Parameter Expansion

One of the most common methods for checking if a variable is set involves using parameter expansion. The syntax ${var+x} will evaluate to nothing if var is unset and will substitute the string x otherwise. This can be used within an if statement to check if a variable is set:

if [ -z ${var+x} ]; then
  echo "var is unset"
else
  echo "var is set to '$var'"
fi

This approach works by checking the length of the string resulting from the parameter expansion. If var is unset, the expansion will result in an empty string, which has a length of zero.

Quotes and Parameter Expansion

It’s worth noting that quotes can be safely omitted when using this syntax because the parameter expansion guarantees that the result will either be an empty string or a single character (x). However, it’s sometimes better to include quotes for clarity and safety:

if [ -z "${var+x}" ]; then
  echo "var is unset"
else
  echo "var is set to '$var'"
fi

Checking for Non-Null/Non-Zero String Variables

To check if a variable is both set and non-empty, you can use the -n test:

if [ -n "$1" ]; then
  echo "You supplied the first parameter!"
else
  echo "First parameter not supplied."
fi

This approach checks the length of the string. If the string is empty (i.e., has a length of zero), the -n test will return false.

Using POSIX Parameter Expansion

POSIX provides several parameter expansions that can be used to check if a variable is set and to provide default values when it’s not:

  • ${parameter:-word}: If parameter is unset or null, use word; otherwise, use the value of parameter.
  • ${parameter:=word}: If parameter is unset or null, assign word to parameter and use its value.
  • ${parameter:?word}: If parameter is unset or null, display an error message including word and exit.

Here’s how these expansions can be used:

echo "${FOO:-hello}"  # Uses "hello" if FOO is unset or empty
FOO=""                # Set FOO to an empty string
echo "${FOO:=world}"  # Sets FOO to "world" and prints it
unset FOO             # Unsets FOO
# echo "${FOO:?error}"  # Would exit with an error message if uncommented

Bash Conditional Expressions

Bash version 4.2 and later support the -v test within conditional expressions, which directly checks if a variable is set:

[[ -v foo ]]; echo $?  # Returns 1 if foo is unset, 0 otherwise
foo="bar"
[[ -v foo ]]; echo $?  # Returns 0 because foo is set

This method is particularly useful in scripts where the set -u or set -o nounset option is enabled, as it avoids the error that would occur when trying to expand an unset variable.

Conclusion

Checking if a variable is set in Bash can be accomplished through various methods, each with its own use cases. Understanding these techniques allows you to write more robust and flexible scripts that handle different scenarios gracefully.

Leave a Reply

Your email address will not be published. Required fields are marked *