Development

How to Check If Bash Variable is Empty

Captain Salem 2 min read

How to Check If Bash Variable is Empty

Bash is a fantastic scripting language that allows performing a plethora of operations, from simple installation scripts to complex log parsers.

However, no matter how simple or complex a bash script turns out, there is one thing they all have in common; variables.

Variables are a fundamental building block in all programming languages. They allow us to store values of various types for later use. Instead of referencing the variable by its memory address, we can use the variable name.

For this tutorial, though, we are going to explore various methods and techniques to determine whether a given bash variable is empty or not.

What is an Empty Variable?

Although the definition of an empty variable may vary depending on the context, an empty variable can be said to be any variable whose value has a length of 0.

Let us now see whether we can determine an empty variable.

Method 1 - Check Variable Length using the -z option

The first and most common method to determine if a variable is empty is by checking its length. We can use the -z flag to accomplish this.

Let us illustrate.

Start by creating a work file:

mkdir scripts
cd scripts

Next, create the bash file:

touch test_var.sh

Finally, add some sample script as shown:

#!/bin/bash
url="geekbits.io"
if [ -z $url]; then
    echo "Variable is empty"
else
    echo "value: $url"
fi

In the example code above, we start by defining a new variable called url. We then use the -z option in the flag command.

The flag command is a conditional evaluation utility that allows you to test various conditional expressions. Using the -z flag, we test if the given string has a length of 0.

If the length is 0, the variable is empty; otherwise, the variable has a value set.

Run the script:

chmod +x test_var.sh
./test_var.sh

The resulting script output:

value: geekbits.io

In this case, since the variable is not empty, the script should print the variable’s value as demonstrated in the output above.

Take a look at the second script provided below:

#!/bin/bash
url=""
if [ -z $url]; then
    echo "Variable is empty"
else
    echo "value: $url"
fi

In the second example above, the url variable is empty. Running the script above should print:

Variable is empty

Method 2 - Check Variable Not Empty

The second method we can use to test if a variable is empty is the -n option from the test flag.

In this case, the -n flag returns True if the length of the string is nonzero.

Consider the example script shown below with an empty value for the url variable:

#!/bin/bash
url=''
if [ -n "$url"];
then
    echo "variable not empty"
else
    echo "variable is empty"
fi

Once we run the script above, we should get the output as shown:

./test_var
variable is empty

Method 3 - Using Direct String Comparison

If you do not wish to use the -z or -n flags, you can perform a direct string comparison as shown in the example below:

#!/bin/bash
url="geekbits.io"
if [[ "$url" == "" ]];
then
    echo "variable is empty"
else
    echo "variable is not empty"
fi

In this case, we directly compare the value of a given variable to an empty string. if the variable is equal to an empty string, we know the variable is empty. Otherwise, the variable has a value set.

Running the script above should return:

./test_variable.sh
variable is not empty

In this case, the url variable is not empty.

Ending…

In this tutorial, you discovered three main methods of determining if a given variable is empty or not using Bash built-in features.

We hope you find this tutorial helpful, if you did, share and leave us a comment down below.

Explore our other tutorials to expand your knowledge.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.