How to check the variable is set or empty in bash

29/12/2020
A variable can be defined or undefined. When any variable is not declared or declared but no value is assigned then the variable is not set or undefined. When any variable is declared and assigned with a value then the variable is set. Many times it requires to know the particular variable is set or not for the programming purposes. One of the important purposes of checking the variable is set or not is data validation. Bash has no built-in function like other standard programming languages to check a variable is set or not. But bash has a feature to do this task. How you can check the variable is set or not in bash is shown in this tutorial.

Syntax:

[[ -v variable ]] Or [[ -z variable ]]

‘-v’ or ‘-z’ option is used to check the variable is set or unset. The above Boolean expression will return true if the variable is set and returns false if the variable is not set or empty.

${variable+string}

Parameter substitute is another way to check the variable is set or unset. If the variable is set, then the value of the string will return otherwise null will return.

Example-1: Check the variable is set or unset using ‘-z’ option

Create a bash file named check_var1.sh with the following script. Here, the first `if` condition will return true and “Num variable is not set” will print. In the next statement, 20 is assigned to the variable, $Num. The second `if` condition will returns false and “Num is set and the value of Num=20” will print.

check_var1.sh

#!/bin/bash
#Check the variable is set or not
if [ -z ${Num} ]; then
  echo "‘Num’ variable is not set"
else
  echo "‘Num’ variable is set"
fi
#Assign a value
Num=20
#Check the variable is set or not after assigning the value
if [ -z ${Num} ]; then
  echo "’Num’ variable is not set"
else
  echo "’Num is set and the value of Num=$Num"
fi

Run the script.

$ bash checkvar1.sh

Example-2: Check the variable is set or unset using parameter substitute

Create a bash file named “check_var2.sh” and add the following script. Here, a string value is assigned to the variable, $str before checking the variable is set or unset. The ‘if’ condition will return true and the message, “’str’ variable is set and the value is Hello” will print.

check_var2.sh

#!/bin/bash
#Set the variable
str=”Hello”
#Assign the value “World” to checkval if the str variable is set
checkval=${str+”World”}
#Check the variable is set or unset
if [ $checkval -eq “World” ]; then
  echo "‘str’ variable is set and the value is $str"
else
  echo "‘str’ variable is not set"
fi

Run the script.

$ bash checkvar2.sh

Example-3: Check the variable is empty or not

Create a bash file named “check_var3.sh” and add the following script. The script will store the first command-line argument into a variable, $argv that is tested in the next statement. The output will be “First argument is empty” if no argument is passed otherwise the value of the first argument will be printed.

check_var3.sh

#!/bin/sh
#Read the first command-line argument value
argv="$1"
#Check the first argument value is provided or not
[  -v "$argv" ] && echo "First argument is empty" ||
echo "The value of the first argument is $argv"

Run the script without any argument.

$ bash checkvar3.sh

Run the script with an argument.

$ bash checkvar3.sh test

Conclusion

Different ways to check the variable is set or unset or empty are shown in this tutorial by using various examples. Hope, this tutorial will help the users to learn the ways of testing any bash variable.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Linux Screen Command Tutorial

In this lesson on Linux Screen Command, we will install and use various commands related to Screen command. The screen...
29/12/2020

30 Bash loop examples

Three types of loops are used in bash for various purposes. These are for, while and until loops. The different uses of...
29/12/2020

How to use Variables in Bash Programming

Variables work as temporary storage for any programming language. The coder needs to have a clear concept of using different...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng Dẫn Chọn Dịch Vụ Thuê Địa Chỉ IPv4
10/03/2025

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024