An empty variable refers to a variable that has been declared but contains no value or has a value that is an empty string (“”). This means that the variable has been assigned, but it doesn’t store any data or holds a null value. In general, Bash offers several conditional expressions with parameters such as -z, -n, =, and !, of which you can choose one to check whether a variable contains nothing or some content.
In this article, I will demonstrate 5 methods to check if a variable is empty/null or not in Bash utilizing different conditional operators.
What is an Empty/Null Variable in Bash?
A variable is a fundamental storage that stores values, manipulates data, and makes the script more dynamic and responsive. It acts as a temporary placeholder that contains different types of data such as strings, integers, floating numbers, or any other data type. If a variable contains no value in it, then it’s called an empty or null variable. Typically, an empty variable appears for two reasons:
- The variable was not initialized.
- The variable was assigned a value at some point, but the value was later cleared.
5 Ways to Check If a Variable is Empty or Not in Bash
Bash provides several conditional tasks to check whether a variable is empty or not, including variable length check, parameter expansion check, double square bracket check, equal operator check, negation operator check, etc. Let’s explore them.
1. Checking Variable Length to Validate if a Variable is Empty or Not
An easy way to check whether a variable is empty/null or not is to check the length of the variable, i.e. whether the string-containing variable is 0 (zero) or greater than 0. In Bash, the -z
and -n
options are used to do the length validation tests of variables.
Using “-z” Option With ‘test’ Command
The “-z” option in ‘if’ statement checks if a string contains zero length, i.e. determines if the assigned variable is empty. If the specified variable length is zero, a True expression is executed.
To check if a variable is empty or null, use the -z
option with the test
command either in the format if [ -z "$variable" ]
or if test -z "$variable"
. Let’s take a look at the following example using the if test -z "$variable"
syntax:
#!/bin/bash
#Defining an empty string variable
xyz=""
#Checking if variable is empty
if test -z "$xyz"; then
echo "The variable is empty."
fi
In the script, the if conditional checks whether the defined variable ‘xyz’ is empty. If the variable is empty, it echoes “The variable is empty.” employing the echo command.
if [ -OPTION "$variable" ]
, remember to add spaces between the single square brackets.Using “-n” Option Within “[ ]” Construct
In the ‘if‘ statement, the -n
option evaluates whether a variable has a value with non-zero length i.e. the variable is not empty. If the variable contains any value, then the test returns true. However, you can use the -n
parameter to check whether a variable that contains a number is not empty. In this case, Bash treats numbers as strings.
For instance, you can do the following non-empty variable testing using the if [ -n "$number" ]
syntax like the below script:
#!/bin/bash
#Defining a variable
number=41
#Checking if variable is not empty using “-n” option
if [ -n "$number" ]; then
echo "The variable is not empty."
fi
Here, the -n
option checks if the defined variable ‘number’ has a value. Since the variable has value 41, the condition evaluates to true and the script prints “The variable is not empty.”
2. Employing Parameter Expansion to Check if a Variable is Empty or Not
Parameter expansion is a strong feature in Bash that is used for a brief validation of variables i.e. whether it is empty or not. The syntax for parameter expansion practiced in empty variable validation is ${variable-}
.
To find out if a variable is empty or not, use the if [ -z "${variable}" ]
& if [ -n "${variable}" ]
syntaxes to execute the output when it becomes true. For example:
#!/bin/bash
#Defining an empty string variable
xyz=""
abc='Hello, Linux!'
#Using parameter expansion to check if the variable is empty
if [ -z "${xyz-}" ]; then
echo "The variable is empty."
fi
#Using parameter expansion to check if the variable is not empty
if [ -n "${abc-}" ]; then
echo "The variable is not empty."
fi
Since two variables are set such as one as an empty string and another with some value in this case, the parameter expansions ${xyz-}
and ${abc-}
evaluate to $xyz
and $abc
and the script prints “The variable is empty.” and “The variable is not empty.” respectively resulting from the conditional check.
3. Using Double Parenthesis “[[ ]]” to Check if a Variable is Empty or Not
To check if a variable is empty or not, use double square brackets “[[ ]]” that offer more enhanced conditional constructs than single square brackets “[ ]” in Bash. This construct works similarly to “[ ]” while doing empty or non-empty variable validation.
For instance, use the syntaxes if [[ -z "${var1}" ]]
& if [[ n "${var2}" ]]
to perform content validation tests on variables.
#!/bin/bash
#Defining variables
var1=""
var2='555'
#Using “-z” option within “[[ ]]”
if [[ -z "${var1}" ]]; then
echo "var1 is empty."
fi
#Using “-n” option within “[[ ]]”
if [[ n "${var2}" ]]; then
echo "var2 contains value."
fi
Here, the conditional expression checks if the variable ‘var1’ is empty and the variable ‘var2’ is not empty using the -z
and -n
options within “[[ ]]” accordingly. If the conditions are satisfied, the script echoes the outputs.
4. Using Equal Operator “=” to Check if a Variable is Empty or Not
The “=” operator is generally used for string comparison, pattern matching, etc. However, this operator can be used within single square brackets “[ ]” to check if a variable is empty by comparing it with an empty string.
Here’s an example of how you can use if [ "$xyz" = "" ]
syntax to check if a variable is empty:
#!/bin/bash
#Defining an empty variable
xyz=""
#Checking if variable is empty using “=” operator
if [ "$xyz" = "" ]; then
echo "The variable is empty."
fi
In the above code, the condition inside the “[ ]” operator checks if the specified variable is equal to an empty string. If the variable is empty, then the condition becomes true and the script displays “The variable is empty.”
5. Using Negation (!) Operator to Check if a Variable is Empty or Not
The “!” operator in Bash is used to reverse the output of a condition. This operator along with the equal “=” operator can be used within single square brackets “[ ]” for empty variable validation.
To check if a variable is empty or not, use if [ "$variable" != "" ]
syntax in the following manner:
#!/bin/bash
#Defining a variable
name="N"
#Checking if variable is not empty using “!=” operator
if [ "$name" != "" ]; then
echo "The variable is not empty."
fi
The ‘if’ condition here compares the specified ‘name’ variable with an empty string and checks if the variable is not equal to the empty string. As the variable contains a value, the condition becomes true and the script shows “The variable is not empty.”
Conclusion
In summary, this article provides you with several options to check whether a variable is empty or not in Bash. You can choose any method according to your preference and use this as a best practice for writing flexible Bash scripts.
People Also Ask
Why check variable emptiness in Bash?
Checking variable emptiness in Bash is crucial for several reasons:
- To ensure proper handling and validation of user input.
- To prevent unintended errors.
- To maintain data integrity.
- To control the conditional decisions.
What’s the difference between checking if a variable is unset and checking if it’s empty?
Checking if a variable is unset confirms whether the variable has been defined or assigned any value. The ‘-v’ operator checks if a variable is set using the syntax if [ -v variable ]
. On the contrary, checking if a variable is empty ensures that the variable contains an empty string. The ‘-z’ operator validates this check using the if [ -z "$variable" ]
.
What happens when a variable is kept empty in Bash?
When a variable is empty in Bash, it means that the variable has no value assigned to it. In such cases, the variable is considered to be null or empty. So, keeping a variable empty may lead to unwanted crash of a program. Also, it leads to unnecessary wastage of memory.
Can I use a single ‘if’ statement to check for both empty and non-empty variables?
Yes, to check for both empty and non-empty variables within the same ‘if‘ statement, you can use the -z
and -n
options. For instance:
#!/bin/bash
#Defining a variable
variable=""
#Checking for empty & non-empty variables
if [ -z "$variable" ]; then
echo "The variable is empty."
elif [ -n "$variable" ]; then
echo "The variable is not empty."
fi