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

Bash brace expansion

Bash uses brace expansion to generate a sequence of strings from the terminal or by using any bash script. A sequence of...
29/12/2020

Bash Head and Tail Command Tutorial

Many types of commands are available in bash to show the content a file. Most commonly used commands are ‘cat’,...
29/12/2020

Bash Range: How to iterate over sequences generated on the shell

You can iterate the sequence of numbers in bash by two ways. One is by using seq command and another is by specifying range...
28/12/2020
Bài Viết

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

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024