How to Check if a Variable is set in Bash?

The “bash” variable is the temporary storage of an integer and the string. Like other programming languages, the bash script allows the users to check whether the variable is set or not. This process is useful for large complex scripts containing multiple variables that are interlinked with each other, and the whole functionality of the script relies on them. 

This guide pens down possible aspects to check whether the variable is set or not in the Bash script.

  • Using the “-v” Option
  • Using the “-z” Option
  • Using the “-n” Option

Method 1: Using the “-v” Option

The “-v” option tells about the declaration of a variable, whether it is declared or not. The basic syntax to use it in a bash script is written below:

Syntax

$ [-v variable name]

Let’s see a “bash” script to check it’s working properly.

Create/Open a Script

There is an “scr3.sh” bash script opened in the “nano” text editor:

$ nano scr3.sh

Code

#!/bin/bash
a="itslinuxfoss"
if [ -v a ]; then
    echo "Variable "a" is set"
else
    echo "Variable "a" is not set"
fi

The “scr3.sh” code description is illustrated here:

  • The “#!/bin/bash denotes the “Shebang line” that instructs the script to execute in “bash” shell. 
  • The variable “a” is declared with a string value “itslinuxfoss”.
  • The “if-then-else-fi” loop defines a conditional statement i.e “-v a”. The -v will return the true result if provided variable “a” will be declared. Otherwise, it will return false.
  • The first echo statement will be executed if the condition becomes true and else the “second” will execute. 

Press “Ctrl+S” to save and “Ctrl+X” to exit the editor.

Execute the “scr3.sh” script:

./scr3.sh

The output shows that variable “a” has been declared.

An Empty Variable

The “-v” option also returns the “true” status if the variable will be empty. It checks whether it was declared previously or not in the bash script. Let’s see it practically.

The above “scr3.sh” script is modified as per requirements:

#!/bin/bash
a=""
if [ -v a ]; then
    echo "Variable "a" is set"
else
    echo "Variable "a" is not set"
fi

At this time the variable “a” contains an empty string i.e nothing:  

Run the script in the terminal:

./scr3.sh

The output is the same as with the non-empty string showing that the mentioned variable is set.

Method 2: Using the “-z” Option

The “-z” is another useful option to check whether the variable is set or not. It performs the same functionality as the “-v” option. Its generalized syntax is a little bit different from “-v” in this way:

Syntax

$ ['-z variable name']

This section explains its working with the help of the “scr3.sh” script contains the following lines of code: 

Code

#!/bin/bash
b="hello"
if ['-z b']; then
    echo "Variable b is set"
else
    echo "Variable b is not set"
fi

The code description is listed below:

  • The variable “b” contains a string value “hello”.
  • The “if-then-else-fi” loop encloses a conditional statement in single quotes i.e ‘-z a’.  
  • The ‘-z’ will return true if the specified variable will have non-zero and zero length same as the “-v” option:

The execution of the “scr3.sh” script displays this result:

./scr3.sh

The output confirms that variable “b” is set.

An Empty Variable

In case the variable contains a zero length, than the “-z” option will also return “true”. Lets’s see it with the above “scr3.sh” script having modified code:

Code 

#!/bin/bash
b=""
if ['-z b']; then
    echo "Variable b is set"
else
    echo "Variable b is not set"
fi

For instance, the “b” variable contains an empty string of zero length:

The output of the “scr3.sh” is displayed here after its successful execution:

./scr3.sh

The output returns the true status i.e “variable b is set”.

Method 3: Using the “-n” Option

The “-n” option is a little bit similar to the above two “-v” and “-z” options. It displays the “true” statement in case of a non-empty variable and the “false” at an empty variable. It follows the stated generalized syntax:

Syntax

$ [[ -n ${variablename} ]]

This syntax encloses the variable name in parameter expansion “${}”. Let’s see its practical implementation via the “scr3.sh” bash script.

Code

#!/bin/bash
a="Linux"
if [[ -n ${a} ]]; then
    echo "Variable "a" is set"
else
    echo "Variable "a" is not set"
fi

In the “scr3.sh” the variable “a” stores a non-empty string. The “if-then-else-fi” defines the condition if “${a}” will set with a non-zero length of the string and then “-n” will display the true condition i.e “Variable a is set ”:

Run the “scr3.sh” script:

./scr3.sh

The output shows that variable “a” is set.

Empty Variable

The “-n” option shows the false statement in case the variable contains an empty string like this code”:

Code

#!/bin/bash
a=""
if [[ -n ${a} ]]; then
    echo "Variable "a" is set"
else
    echo "Variable "a" is not set"
fi

Execute the script and shows its output in the terminal:

./scr3.sh

The output clears that variable “a” is not set due to its empty length of the string.

Conclusion

Bash provides the “-v”, “-z”, and “-n” options to check whether the variable is set or not. These options are used in the conditional statement of the “if-then-else-fi” loop. The “-v” and “-z” options return the “true” status if a variable contains a non-zero and zero length of a string. On the other hand the “-n” shows “true” at non-zero length and “false” at the “zero” length of a string.

This guide has provided all possible aspects to check if a variable is set in Bash.