Bash: Determinate state of variable


A variable in bash (and any POSIX-compatible shell) will be in one of the three following states:

  • unset
  • set but empty
  • set and not empty

Variables in bash do not have data types. A variable can contain a number, a character, a string of characters or the empty string. Assigning a value to the reference of a variable will create it, you do not need to declare it beforehand.

Following, you will find some tests that allow you to find which state the variable is in. They will allow you to distinguish between ‘unset’ and ‘set but empty’ states.

# Test if VAR is 'unset' or 'set but empty'
if [ -z "${VAR}" ]; then
	echo "VAR is 'unset' or 'set but empty'";
fi

# Test if VAR is 'unset'
if [ -z "${VAR+BFNET}" ]; then
	echo "VAR is 'unset'";
fi
# Where ${VAR+BFNET} is a parameter expansion which evaluates to null if VAR is 'unset',
# if VAR is set to anything (including the empty string) the construct expands to BFNET.

# Test if VAR is 'set but empty'
if [ -z "${VAR-BFNET}" ]; then
	echo "VAR is 'set but empty'";
fi
# Where ${VAR-BFNET} is a parameter expansion which evaluates to the value of VAR if VAR is 'set' (either empty or not),
# if VAR 'unset' the construct expands to BFNET.
# This is useful for providing default values
# For example:
#function custom_echo () {
#    local DEFAULT_MSG="No message passed."; # Doesn't really need to be a local variable.
#    MESSAGE=${1:-$DEFAULT_MSG}; # Defaults to default message.
#    echo -e "$MESSAGE";
#    return;
#}
# In the above function if the user does not provide the message to be printed with the function call, then the default message will be used.

# Test if VAR is 'set and not empty'
if [ -n "${VAR}" ]; then
	echo "VAR is 'set and not empty'";
fi

Following is a truth table for the above:

                        +-------+-------+--------+
                VAR is: | unset | empty | filled |
+-----------------------+-------+-------+--------+
| [ -z "${VAR}" ]       | true  | true  | false  |
| [ -z "${VAR+BFNET}" ] | true  | false | false  |
| [ -z "${VAR-BFNET}" ] | false | true  | false  |
| [ -n "${VAR}" ]       | false | false | true   |
+-----------------------+-------+-------+--------+

This post is also available in: Greek

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.