The if statement in bash scripting refers to controlling a script’s flow, and sometimes multiple scripts depend on the value of a single variable. It can cause dependency issues as the variable isn’t even initialized. To see if a variable exists or not cannot be done solely using the if command, but using the -v and -z flags is possible.
This guide illustrates how to check whether a variable exists in a “Bash if statement.”
- Using the -v Flag
- Using the -z Flag
Method 1: Use the -v Flag to Check if a Variable Exists in an if Statement
The flag “-v” is used with the “if statement” to check if the variable exists and has a non-empty value and here is the code that is used in the following example:
#!/bin/bash
first=10
if [[ -v first ]];
then
echo "The first number is set to $first"
else
echo "First number is not set"
fi
# b: variable is not set
if [[ -v second ]];
then
echo "The second number is set to $second"
else
echo "Second number is not set"
fi
It is named “script.sh” viewed using the nano editor as in this way:
$ nano script.sh
Here is the explanation of the above script:
- “-v” flag in the “if statement” that checks if the variable exists and is not empty.
- “then” shows the output “The first number is set to $first” if the variable exists and has a value.
- “else” shows the message “First number is not set” only if the number does not exist or has an empty value.
- After that, the second number is checked and kept empty.
To execute the above script, make it executable using this command:
$ sudo chmod +x script.sh
More details on the chmod command can be read in this guide.
Let’s execute this script to see the results:
$ bash script.sh
As expected, it displayed the messages according to the set conditions but did not display the error, which is why it is recommended to use it to check the scripts before executing them.
Method 2: Use the -z Flag to Check if a Variable Exists in an if Statement
The “if command,” when used with the “-z” flag, only checks for an empty string, and it could be helpful in situations where it is required to check if the input is empty or not. TRUE is returned when the string length is zero.
Here is a snippet of the code being used:
#!/bin/bash
first=10
if [[ -z ${first} ]];
then
echo "Second number is not set"
else
echo "The second number is set to $first"
fi
Here is the explanation of the above script:
- “-z” flag checks the “first” variable which is not empty so the condition is false and “then” condition is ignored
- The “else” block’s output will be displayed because the “first” variable is not empty
View and edit it using the nano editor in this way and it is named “script.sh”:
$ nano script.sh
Let’s execute it to see the results:
$ bash script.sh
As expected, it displayed the messages according to the set conditions.
Conclusion
The if statement cannot check whether the variable exists; instead, it will throw an error. To check it, the “-v” flag is optimal. However, if you wish to check if the variable has no or zero value, the “-z” flag is recommended. Bash provides a wide variety of features and functionalities, and sometimes users can encounter errors with everything discussed in these guides.
This guide explained how to check if a variable exists in a bash if statement.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY