This tutorial is about checking variables in bash shell script programming for

  • Check variable is set or not
  • variable is empty or nonempty
  • Check for the variable is an empty string or not

Here are some examples of variable usage for variable price

Variable declaration Description
price; variable is not declared and unset
price=; variable is declared and not unset
price=3000 variable is declared,assigned and not unset
price=(3000) variable is declared,assigned and not unset
unset price; variable is not declared and unset

How to check if a variable is set in a bash script?

For example, the variable is set means,

  • It is declared and assigned with empty or non-empty.

In this below example,

  • variable1 is declared but empty
  • variable2 is not declared and not set.
#!/bin/bash  
variable1=""
if [[  ${variable1+x}  ]]
then
  echo 'variable1 is set'
else
  echo 'variable1 is not set'
fi

if [[  ${variable2+x}  ]]
then
  echo 'variable2 is set'
else
  echo 'variable2 is not set'
fi

Output:

variable1 is set
variable2 is not set

Another way to check variable is set using -v option

variable1=""
if [ ! -v $variable1 ] 
then
    echo "variable1 is unset"
else
    echo "variable1 is set"

fi

Output:

variable1 is unset

How to check if the variable is unset in the bash script?

For example, the variable is unset means

  • It does not exist and is not declared.

In this below example,

  • variable1 is declared but empty
  • variable2 is not declared and unset.
#!/bin/bash  
variable1=""
if [[ ! ${variable1+x}  ]]
then
  echo 'variable1 is not set'
else
  echo 'variable1 is set'
fi

if [[ ! ${variable2+x}  ]]
then
  echo 'variable2 is not set or unset'
else
  echo 'variable2 is  set'
fi

Output:

variable1 is set
variable2 is not set or unset

How to check variable is empty or non-empty

This tutorial checks for check for variable compared with empty space and wrap this expression inside [[]].

variable1=""
if [[ $variable1 = "" ]]
then
  echo 'variable1 is empty'
else
  echo 'variable1 is not empty'
fi

The same also can be written using variable in double quotes wrapped in single bracket[].

variable1=""
if [ "$variable1" = "" ]
    then
    echo "variable1 is empty"
else
  echo 'variable1 is not empty'
fi

Output:

variable1 is empty

Let us also check not to empty using! operator.

Here is a code for example checks if a variable is non-empty.

One way,

variable1=""
if [[ ! $variable1 = "" ]]
then
  echo 'variable1 is empty'
else
  echo 'variable1 is not empty'
fi

Another way,

variable1=""
if [ ! "$variable1" = "" ]
then
  echo 'variable1 is empty'
else
  echo 'variable1 is not empty'
fi

Another use -z option to check variable is set and empty or non-empty using below code

variable2=""
variable3="test"

if [ -z "$variable2" ]
then
    echo "variable2 is set and empty"
else
    echo "variable2 is set and non empty"
fi

if [ -z "$variable3" ]
then
    echo "variable3 is set and empty"
else
    echo "variable3 is set and non-empty"
fi

Output:

variable2 is set and empty
variable3 is set and non-empty