How to Check if Variable Is Empty in Bash

Fumbani Banda Feb 02, 2024
  1. Using the -z Option to Check if Variable Is Empty in Bash
  2. Using the -n Option to Check if Variable Is Empty in Bash
  3. Check if a Variable Is Empty in Bash - Compare With Empty String
  4. Check if a Variable Is Empty in Bash - Check With Replace Method
How to Check if Variable Is Empty in Bash

This tutorial illustrates checking if a variable is empty in bash using the test command with the -z and -n options.

Using the -z Option to Check if Variable Is Empty in Bash

We use the test command with the -z option. The -z option checks if the length of the string variable is 0.

If the length of the string variable is 0, the test returns true, and the script prints to the standard output that the string variable is empty. And if the length of the string variable is not 0, the script prints that the string variable is not empty.

The greet variable has a string assigned to it in the case below. During the test, the greet variable is checked if the length of string value it stores has the length of 0.

Since the greet variable has the string Hi, which has two characters assigned to it, the test returns false, and the script prints that the greet variable is not empty to the standard output.

greet='Hi'

if [ -z "$greet" ]
then
    echo "\$greet is empty"
else
    echo "\$greet is not empty"
fi

Output:

$greet is not empty

The greet variable is assigned to an empty string in the script below. The greet variable is checked using the test/[ command to see if the length of its string value is 0.

Since the greet variable is assigned to an empty string, the test returns true, and the script prints to the standard output that the greet variable is empty.

#!/bin/bash

greet=''

if [ -z "$greet" ]
then
    echo "\$greet is empty"
else
    echo "\$greet is not empty"
fi

Output:

$greet is empty

Using the -n Option to Check if Variable Is Empty in Bash

The script below uses the test command with the -n option to check whether a string variable is empty. The -n option checks if the length of the value in the string variable is nonzero.

If the length of the string in the variable is not zero, the test returns true, and it prints that the variable is not empty. If the length of the string variable is zero, the test returns false, and it prints that the variable is empty.

The greet variable is assigned to an empty string in the script below. When the greet variable is checked using the test command, if the length of the string it stores is nonzero, it returns false, and the script executes the command in the else section.

#!/bin/bash

greet=''

if [ -n "$greet" ]
then
    echo "\$greet is not empty"
else
    echo "\$greet is empty"
fi

Output:

$greet is empty

The greet variable has been assigned to Hi, a string with two characters in the script below.

The test that checks if the length of the string in the greet variable is nonzero returns true because the greet variable is assigned to a string with two characters. The script prints that the greet variable is not empty to the standard output.

#!/bin/bash

greet='Hi'

if [ -n "$greet" ]
then
    echo "\$greet is not empty"
else
    echo "\$greet is empty"
fi

Output:

$greet is not empty

Check if a Variable Is Empty in Bash - Compare With Empty String

We can check if the value is empty by comparing it with "".

x="Non-empty variable"
if [[ "$x" == "" ]]; then
    echo "x is empty"
else
    echo "x is not empty"
fi

Check if a Variable is Empty in Bash - Compare With Empty String

Check if a Variable Is Empty in Bash - Check With Replace Method

If x is defined, the expression is replaced with test, otherwise null.

if [ ${x:+test} ]; then
    echo "x is not empty"
else
    echo "x is empty"
fi

Check if a Variable is Empty in Bash - Compare With Empty String

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Bash Variable