Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Very often in our shell scripts, we need to check if a variable is empty. It could be to validate the input parameters passed to a function or to execute a different action based on the value in a variable.

In this tutorial, we’ll look at different ways to check whether a variable is empty. We’ll look at different connotations for emptiness as well.

2. Empty Variable

Based on the context, the meaning of the word “empty” can vary.

Let’s say we’ve got a variable NAME. There can be three possible cases based on the value inside this variable:

  1. the variable is unset
  2. the variable is set but empty
  3. the variable has some value in it

Let’s check how we can distinguish between these scenarios during scripting.

3. Using Length Check

First, let’s look at the simplest way of checking whether a variable is empty. Typically, we use length checking to see if a variable is empty or not.

Let’s look at an example:

$ cat check.sh 
NAME=""
if [ -z $NAME ]; then
    echo "Name is empty"
else
    echo "Name is $NAME"
fi
$ ./check.sh 
Name is empty

Here, in the script we’ve got a variable NAME set to an empty string. Then, in the if condition we’ve used the -z option to check whether the string is empty. Finally, when we ran the script, it printed that the variable is empty.

4. Using the Not Empty Check

Another simple way to test is using the not-empty check:

$ cat empty.sh 
NAME=""
if [ -n "$NAME" ]; then
    echo "Name is $NAME"
else
    echo "Name is empty"
fi
$ ./empty.sh 
Name is empty

This is similar to the script we saw earlier. But, in the if condition, we’ve used the -n option to test for string being not empty.

The above two methods are the simplest way to check if a variable is empty. But in some cases, we might need to check whether a variable is declared. Next, we’re going to look at how we can handle this case.

5. Using Brace Expansion

In this section, we’ll look at the different ways to check if a variable is unset. As we know, when a variable is not declared, we call it unset.

Brace expansion, to put it simply, is a technique for manipulating strings to produce new ones as we need. There are various options for brace expansion.

We’ll look at these three different syntaxes:

  • ${variable-value}: to set default value
  • ${variable=value}: to assign a value
  • ${variable+value}: to set another value

This syntax allows testing of the variable for unset. Let’s look at some examples to understand how this works.

5.1. Set Default Value

First, let’s take a look at setting a default value:

$ bash -c 'echo "${NAME-x}"; echo $NAME'
x

$ bash -c 'NAME=""; echo "${NAME-x}"; echo $NAME'


$ bash -c 'NAME="y"; echo "${NAME-x}"; echo $NAME'
y
y

We’ve got a set of three commands here. There, we’re trying to expand the variable NAME.

For the first one, we’ve got the variable NAME unset. Then, the value x is substituted. However, the variable itself remains unchanged. We can see the outcomes of the echo commands are consistent with this concept.

In the second one, we’ve got the variable set to an empty string. Then, the value x is ignored during the parameter expansion. Also, the variable NAME remains unchanged. Consequently, both echo commands printed empty lines.

Lastly, we can see the variable has a non-empty value. Then the value inside the variable is used everywhere.

In short, when the variable is unset, it doesn’t allow substitution.

5.2. Assign a Value

Next, let’s take a look at assigning a value:

$ bash -c 'echo "${NAME=x}"; echo $NAME'
x
x
$ bash -c 'NAME=""; echo "${NAME=x}"; echo $NAME'


$ bash -c 'NAME="y"; echo "${NAME=x}"; echo $NAME'
y
y

Using the = operator, we’ve achieved different results. Let’s look at them one by one. In the first command, the variable NAME is not declared. Then using parameter expansion, we can see the variable NAME is declared and set with the given value.

The second command behaved just the opposite. We could see that upon declaring the variable, it neither accepted a new value nor took the supplied value.

The third command also behaved exactly the same as the second one. Since the variable is declared and set with a value, it didn’t take the new value.

In short, if the variable is unset then it assigns the value.

5.3. Set a Different Value

Lastly, let’s check the case of assigning a different value:

$ bash -c 'echo "${NAME+x}"; echo $NAME'


$ bash -c 'NAME=""; echo "${NAME+x}"; echo $NAME'
x

$ bash -c 'NAME="y"; echo "${NAME+x}"; echo $NAME'
x
y

Here, with the + operator, we could see when the variable is unset, the value is ignored.

But, if the variable is not empty, it does substitute the value but doesn’t change the value inside the variable.

5.4. Check for Unset Variable

Armed with this technique of parameter expansion, let’s look at how we can use it for checking unset variables.

$ bash -c '[ -z "${NAME+x}" ] && echo "Not set"'
Not set
$ bash -c 'NAME=""; [ -z "${NAME+x}" ] && echo "Not set"'
$ bash -c 'NAME="a"; [ -z "${NAME+x}" ] && echo "Not set"'
$
$ bash -c '[ x = "${NAME=x}" ] && echo "Not set"'
Not set
$ bash -c 'NAME=""; [ x = "${NAME=x}" ] && echo "Not set"'
$ bash -c 'NAME="A"; [ x = "${NAME=x}" ] && echo "Not set"'
$
$ bash -c '[ x = "${NAME-x}" ] && echo "Not set"'
Not set
$ bash -c 'NAME=""; [ x = "${NAME-x}" ] && echo "Not set"'
$ bash -c 'NAME="a"; [ x = "${NAME-x}" ] && echo "Not set"'
$

In the above set of commands, we see that the output is printed only when the variable is not declared. This is done for all three parameter expansion syntaxes.

6. Using the -v Option

Next, let’s see a very straightforward option to check if a variable is unset. This is by using the -v conditional expression. However, this is only supported in Bash from version 4.2 and above.

Let’s see this in action:

$ bash -c '[ -v NAME ] || echo "Not set"'
Not set
$ bash -c 'NAME=""; [ -v NAME ] || echo "Not set"'
$ bash -c 'NAME="a"; [ -v NAME ] || echo "Not set"'

Here, we can see the check fails when the variable is not declared. Also, we should note that we’ve to use the name of the variable in the conditional and not the value in it.

7. Using the Double Brackets

In some cases, we prefer to use the [[ syntax for checking conditionals. This is a new and much-improved version of the [ command.

Checking for an empty variable is similar to that of the [ command:

$ bash -c '[[ ! -n $NAME ]] && echo "Name is empty"'
Name is empty
$ bash -c '[[ -z $NAME ]] && echo "Name is empty"'
Name is empty

Above, we’ve used two checks. The first one checks for a nonempty string and negates the result. And it correctly prints that the variable NAME is empty.

The second check is the same as the earlier one. And it prints the result correctly.

We should note that when using the [[ syntax, we’ve omitted quoting the variable and this is perfectly fine. However, if compatibility is a concern, then we should refrain from using this.

8. Using the set -u Option

There’re various options available in shell scripting to enable different control flows. There exists a setting to exit when the script tries to access an unset variable. This is the -u or the nounset option.

Let’s check how to use it:

$ cat empty.sh 
#!/bin/bash
set -u
echo "$NAME"
$ ./empty.sh 
./empty.sh: line 3: NAME: unbound variable

Here, we’ve got a script that sets the -u option. Then while trying to access an unset variable NAME the script exited with an error.

We can use this option when we need to be very strict with our variables. This could help us during our development and ensure we set default values for the unset variables.

9. Conclusion

In this article, we’ve seen different ways to check for an empty variable. We’ve seen three different cases of emptiness. We may use them depending upon our use cases.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!