How To Check if Bash Variable is Empty? [2 Easy Methods]

Programmers work with several variables in the Bash scripts. So verifying whether a variable is empty or not is a crucial task in Bash scripting. By ensuring the emptiness of variables, programmers can handle different scenarios and enhance the reliability of their scripts. This article explores various techniques and commands for checking if a Bash variable is empty or not. So, gain the skills to write robust and error-resistant code.

Key Takeaways

  • Getting familiar with the process of checking whether a bash variable is empty using the -n flag and double bracket.
  • Knowing about the usage of the double bracket to check whether a Bash variable is empty.

Free Downloads

2 Methods to Check Whether Bash Variable is Empty

Checking whether a Bash variable is empty or not is a crucial task for any programmer. Here, I will show you two methods of checking if the Bash variable is empty.

You can read our Comparative Analysis of Methods to distinguish between these two methods and pick the best one for your needs.

Method 01: Using Flag “-n” to Check Bash Variable Whether It is Empty

Checking a Bash variable whether it is empty or not, is a crucial task. Here, I have developed a Bash script that can perform the task with if condition and. To do so, follow the below procedures.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano varchk.sh
EXPLANATION
  • nano: Opens and creates a file in the nano text editor.
  • varchk.sh: File name.

❸ Copy the script mentioned below:

#!/bin/bash

var="" #NULL is set as the value of var variable

if [ -n "$var" ]; then
echo "Variable is $var" #check the value of var variable if found, then print this line with the value of var on the terminal

else

echo "Variable is empty" #if the value of var is not found means it is empty, then prints this line on the terminal

fi
EXPLANATION

#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then var=”” command set NULL as the value of the var variable. Then, if [ -n “$var” ]; then echo “Variable is $var” command checks the value of the var variable if found then print this line with the value of var on the terminal. If the variable is empty then the else echo “Variable is empty” command prints this line on the terminal.

❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.

➎ Run the script by the following command:

bash varchk.sh
EXPLANATION
  • bash: Executes the bash scripts.
  • varchk.sh: Bash script name.

The Bash script has checked with if condition and printed on the terminal that the variable is empty.The above image shows that I have checked the variable and printed the message “Variable is empty” on the terminal using a Bash script.

Method 02: Using Double Brackets to Check if a Bash Variable is Empty

In this section, I will develop a Bash script using double brackets to check if a Bash variable is empty. To know about the process, follow the below script.

You can follow the steps mentioned in method 1 to know how to write, execute and run the bash script.

Script (double.sh) >

#!/bin/bash

NAME="" #NAME variable is set

# Define the command to check if the variable is empty
check_empty_command='[[ ! -n "$NAME" ]] && echo "Name is empty"'

# Execute the command using bash -c
bash -c "$check_empty_command"
EXPLANATION

The NAME=”” command set NULL to the NAME variable. Then the check_empty_command variable is defined. Then the bash -c “$check_empty_command” command checks whether the variable is empty if so, prints a message on the terminal.

Run the script by the following command:

bash double.sh

The Bash script has checked and printed that the variable is empty.The bash script has checked and printed the message that the variable is empty.

Comparative Analysis of the Methods

In this article, I have shown you two methods of checking whether a Bash variable is empty. Now I will show you a comparative analysis of these two methods.

Methods Pros Cons
Method 01
  • Easy if the condition is used.
  • Little bit lengthy script.
Method 02
  • Comparatively short script.
  • A comparatively complex command is used.

If you want to create a Bash script with easy-to-understand commands you can follow method 1. But if you need to create a concise script, you can follow method 2.

Conclusion

In conclusion, checking if a Bash variable is empty is essential in shell scripting. By using a double bracket with the ‘-z’ operator or the ‘-n’ operator, we can easily determine if a variable is empty or not. These techniques provide effective ways to verify empty variables, ensuring robust and accurate scripts. Mastering variables checks allows for better data validation and script reliability. Remember to consider variable expansion and quoting to ensure accurate evaluations. With these methods, you can confidently check empty variables in Bash scripts.

People Also Ask

How do you check if a variable is empty?
The variable’s status is determined by the empty() method. If the variable is present and not empty, this method returns false; otherwise, it returns true. The values 0 and the following evaluate to nothing.
How to check if a variable has a value in Bash?

The -n option returns false if the value is specified but empty, much like the -z option does. The -n option returns false if the variable is defined but has no value, in contrast to the -v option. The -n option returns true if the value of the variable is not empty, much like the -v option does.

How to check equality in bash?
Programmers use the == operator with the [[ command, which takes input from the user and then compares the specified strings. At first, the script prompts for the two strings and then outputs the result.
How to check multiple conditions in bash?
The common way of having multiple conditions with the if statement is to use the double bracket notation. [[ EXPR1 && EXPR2 ]] True if both EXPR1 and EXPR2 are true. [[ EXPR1 || EXPR2 ]] True if either EXPR1 or EXPR2 is true.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment