What Does -z Mean in Bash?

Bash is the most popular programming environment in Linux that is utilized for creating scripts to perform tasks. The “-z” is the operator in the Bash script that is mainly considered to obtain the results if the variables are empty. It is beneficial for the user to perform/generate tasks based on the results.

The subsequent post will cover the “z” operator in the bash script with help of examples.

  • The “-z” Flag in Bash Script
  • Check the Empty Variable
  • Check the Passed Arguments 

The “-z” Operator in Bash Script

As mentioned earlier, the “-z” checks the emptiness of the variable. Here’s the syntax that the user considers to use the “-z” operator in the Bash script.

Syntax

if [ -z <variable> ]
then
Command to execute
else
Command to execute
fi

The syntax is defined as 

  • Use the “if” condition and define the <variable> to check if it is empty through the “z” operator.
  • The “then” portion performs tasks if the condition is true.
  • The “else” portion executes commands if the condition fails.

Example 1: Check the Empty Variable

The following script checks the variable is empty and prints the appropriate message to the user:

#!/bin/bash

var1=''
if [ -z $var1 ]
then
echo "Variable is Empty "
else
echo "Variable is Not Empty"
fi

The script is defined as 

  • Empty variable “var1” is initialized.
  • The “if” condition to check the variable “var1” is empty using the “z” operator.
  • The “then” portion prints the echo statement if the condition is true.
  • The “else” portion prints the echo statement if the condition fails.

Save the script in the file and exit.

Run the script file through the bash command:

$ bash script.sh

Because the variable is empty, the message “variable is empty” is printed on the screen.

Example 2: Check the Passed Arguments 

The below script checks if the specific argument passed to the Bash script is empty or not:

#!/bin/bash

if [ -z $1 ]
then
echo "First Argument is Empty"
elif [ -z $2  ]
then
echo "Second Argument is Empty"
elif [ -z $3 ]
then
echo "Third Argument is Empty"
else
echo "All Arguments Are Non-Empty"
fi

The script is defined as

  • The “if” statement to check if the first argument passed is empty, stores in the $1 variable and prints the message.
  • The “elif” statement to check if the second argument passed is empty, stores in the $2 variable and prints the message.
  • Another “elif” statement to check if the third argument passed is empty stores in the $3 variable and prints the message.
  • The “else” portion prints the echo statement and all arguments are non-empty.

Save the above script file and exit.

Let’s run the script file and check all cases one by one:

$ bash script.sh "" 2 3
$ bash script.sh 1 "" 3
$ bash script.sh 1 2 ""
$ bash script.sh 1 2 3

The output is defined as 

  • The first argument sent ( “” 2 3)was empty and the message is printed accordingly
  • Then next time, the second argument sent ( 1 “” 3) was empty and the message is printed accordingly
  • Similarly, the third argument sent ( 1 2 “” ) was empty and the message is printed accordingly
  • Lastly, all non-empty arguments are passed (1 2 3) and the message is printed accordingly.

Conclusion

The “z” is the operator in the Bash script considered for checking whether the variable is empty or not utilized in the “if” conditions. The user can check any variable defined in the Bash script or check the passed arguments to the Bash script.

This post has answered the query “what does -z mean in Bash” with help of examples.