check if variable is set in shell script

How to Check if Variable is Empty or Not in Shell Script

Shell scripts often use many variables. Some of them have values assigned to them while others may be empty. If you call commands on empty variables, you are likely to see an error message. So it is advisable to check if a variable is empty or not in shell script, before you work with them. In some cases, you may also need to run different code snippets depending on whether variable is set or not. In this article, we will learn how to check if variable is empty or not in shell script.


How to Check if Variable is Empty or Not in Shell Script

There are several ways to check if variable is empty or not in shell script. We will look at some of them.


1. Using -z operator

You can easily check if a variable is empty or not by adding -z operator before it, as shown below. Here is an example to check if variable $var is set or not.

-z "$var"

The above operator checks if the length of variable $var is zero, and returns True if it is zero, else it returns False. You can also use it in if conditions as shown below.

if [ -z "$var" ]
then
      echo "\$var is empty"
else
      echo "\$var is NOT empty"
fi

You can also combine the -z operator check with AND (&&) and OR (||) operators to directly display whether a variable exists or not.

[ -z "$var" ] && echo "Empty"
[ -z "$var" ] && echo "Empty" || echo "Not empty"

In the first statement above, the result of conditional expression is combined with echo statement using AND operator. So if it evaluates to true, only then the string is echoed. Else nothing is displayed. In the second statement, we use an OR operator to display string “Not empty” if the conditional expression evaluates to False.

You can also check if a variable is not set by adding a negation operator before -z operator.

if [ ! -z "$var" ]
then
      echo "\$var is NOT empty"
else
      echo "\$var is empty"
fi

Similarly, you can use negation operator in conditional expressions as shown below.

[ ! -z "$var" ] || echo "Empty"
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"


2. Using test operator

You can also use test command with -z operator, to check if a variable is empty or not. Here is an example.

if test -z "$var" 
then
      echo "\$var is empty"
else
      echo "\$var is NOT empty"
fi

In the above if-else statement, if the variable is empty then the test command evaluates to True, else it evaluates to False.

You can use the above commands in shell terminal or shell script. They work fine on all shells on all Linux distributions.

In this article, we have learnt how to easily determine if a variable is empty or not.

Also read:

How to Check if File Exists in Shell Script
How to Check if Directory Exists in Shell Script
How to Check if Folder Exists in Python
How to Get Directory of Shell Script
How to Create Multiline String in Python

Leave a Reply

Your email address will not be published. Required fields are marked *