How to check if a variable is set or empty in bash

A variable can be defined or undefined. If any variable is not declared or declared but not assigned a value, then the variable is not set or not defined. When any variable is declared and assigned a value, then the variable is set. In many cases, for programming purposes, you need to know if a particular variable has been set. One of the important purposes of checking whether a variable is set or not is data validation. Bash does not have a built-in function like other standard programming languages ​​to check if a variable is set. But bash has a function to accomplish this task. How you can check if a variable is set in bash is shown in this article.

Syntax:

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

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

${variable+string}

Parameter substitution is another way to check if a variable is set or not. If the variable is set, then the string value will be returned, otherwise null will be returned.

Example-1: check if a variable is set or not using the -z option

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

check_var1.sh

#!/bin/bash #
Проверить, установлена ли переменная
if [ -z ${Num} ]; then
echo "Переменная 'Num' не установлена"
else
echo "Переменная 'Num' установлена"
fi
# Назначить значение
Num=20
# Проверить, установлена ли переменная после присвоения значения
if [ -z ${Num} ]; then
echo "Переменная 'Num' не установлена"
else
echo "'Num установлено и значение Num=$Num "
fi

Run the script.

$ bash checkvar1.sh

Example-2: check if a variable is set or not set using parameter substitution

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

check_var2.sh

#!/bin/bash
# Установить переменную
str = ”Hello”
# Назначить значение “World” для checkval, если переменная str установлена
checkval = $ {str + “World”}
# Проверить, установлена ли переменная,
if [ $checkval -eq “World” ]; then
echo "переменная 'str' установлена и значение равно $str"
else
echo "переменная 'str' не установлена"
fi

Run the script:

$ bash checkvar2.sh

Example-3: Check if a 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 in the $ argv variable, which is checked 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
# Прочитать значение первого аргумента командной строки
argv="$1"
# Проверить, указано ли значение первого аргумента
[  -v "$argv" ] && echo "Первый аргумент является пустым" ||
echo "Значение первого аргумента равно $argv"

Run script with no arguments.

$ bash checkvar3.sh

Run the script with an argument.

$ bash checkvar3.sh test

Output

In this article, various examples show various ways to check that a variable is set, unset, or empty. Hopefully this article will help users learn how to test any bash variable.

Sidebar