How to determine if a bash variable is empty?

Solution 1:

This will return true if a variable is unset or set to the empty string ("").

if [ -z "${VAR}" ];

Solution 2:

In Bash, when you're not concerned with portability to shells that don't support it, you should always use the double-bracket syntax:

Any of the following:

if [[ -z $variable ]]
if [[ -z "$variable" ]]
if [[ ! $variable ]]
if [[ ! "$variable" ]]

In Bash, using double square brackets, the quotes aren't necessary. You can simplify the test for a variable that does contain a value to:

if [[ $variable ]]

This syntax is compatible with ksh (at least ksh93, anyway). It does not work in pure POSIX or older Bourne shells such as sh or dash.

See my answer here and BashFAQ/031 for more information about the differences between double and single square brackets.

You can test to see if a variable is specifically unset (as distinct from an empty string):

if [[ -z ${variable+x} ]]

where the "x" is arbitrary.

If you want to know whether a variable is null but not unset:

if [[ -z $variable && ${variable+x} ]]

Solution 3:

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

  • unset
  • set to the empty string
  • set to a non-empty string

Most of the time you only need to know if a variable is set to a non-empty string, but occasionally it's important to distinguish between unset and set to the empty string.

The following are examples of how you can test the various possibilities, and it works in bash or any POSIX-compatible shell:

if [ -z "${VAR}" ]; then
    echo "VAR is unset or set to the empty string"
fi
if [ -z "${VAR+set}" ]; then
    echo "VAR is unset"
fi
if [ -z "${VAR-unset}" ]; then
    echo "VAR is set to the empty string"
fi
if [ -n "${VAR}" ]; then
    echo "VAR is set to a non-empty string"
fi
if [ -n "${VAR+set}" ]; then
    echo "VAR is set, possibly to the empty string"
fi
if [ -n "${VAR-unset}" ]; then
    echo "VAR is either unset or set to a non-empty string"
fi

Here is the same thing but in handy table form:

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

The ${VAR+foo} construct expands to the empty string if VAR is unset or to foo if VAR is set to anything (including the empty string).

The ${VAR-foo} construct expands to the value of VAR if set (including set to the empty string) and foo if unset. This is useful for providing user-overridable defaults (e.g., ${COLOR-red} says to use red unless the variable COLOR has been set to something).

The reason why [ x"${VAR}" = x ] is often recommended for testing whether a variable is either unset or set to the empty string is because some implementations of the [ command (also known as test) are buggy. If VAR is set to something like -n, then some implementations will do the wrong thing when given [ "${VAR}" = "" ] because the first argument to [ is erroneously interpreted as the -n operator, not a string.


Solution 4:

-z is a the best way.

Another options I've used is to set a variable, but it can be overridden by another variable eg

export PORT=${MY_PORT:-5432}

If the $MY_PORT variable is empty, then PORT gets set to 5432, otherwise PORT is set to the value of MY_PORT. Note the syntax include the colon and dash.


Solution 5:

If you're interested in distinguishing the cases of set-empty versus unset status, look at the -u option for bash:

$ set -u
$ echo $BAR
bash: BAR: unbound variable
$ [ -z "$BAR" ] && echo true
bash: BAR: unbound variable
$ BAR=""
$ echo $BAR

$ [ -z "$BAR" ] && echo true
true

Tags:

Scripting

Bash