It is good to note that since ZSH is built on top of Bash, the syntax and functionality are similar for scripts built for Bash or ZSH shell.
If Statement
The most basic way to implement conditional logic to a shell script is by using an if statement. The general syntax for an if statement in ZSH is below:
if condition
then
// run commands
fi
To implement an if statement, we start by calling the if keyword. We then pass the conditional expression to evaluate. The conditional expression must return a Boolean value.
After the conditional expression comes to the then keyword and fi block, inside these blocks, we pass the commands to run if the conditional expression evaluates to true.
If the conditional expression passed evaluates to a Boolean False, the block of commands inside the then fi block gets ignored, and the conditional exits.
Although not required, it is recommended to indent your code blocks for readability and maintenance.
Consider the example if statement below:
#!/usr/bin/zsh
if [[ 100 -gt 50 ]]
then
echo "100 is greater than 50"
fi
Save the file and close.
Next, make the file executable using the chmod command as:
Finally, run the script as:
The script will run the if block and check if 100 is greater than 50. If true, it will run the echo command and exit.
Below is an example output:
100 is greater than 50
If..Else Statement
A more evolved version of the if statement is the if..else statement. It works like the if statement but adds a handle if the conditional expression evaluates to false.
Below is the general syntax for the if..else statement:
if conditional
then
// commands to run if true
else
// commands to run if false
fi
As seen from the example above, if the conditional expression evaluates to true, the block between then and else statements executes. However, if false, the else block executes.
Consider the example shown below:
echo -n "Enter a number: "
read num
if [[ $num -gt 50 ]]
then
echo "$num is greater than 50"
else
echo "$num is not greater than 50"
fi
In the example script, we ask the user to enter a number. If the number is greater than 50, we echo that the number is greater than 50. If false, we echo the number is not greater than 50.
Below is an example output:
Enter a number: 10
10 is not greater than 50
$ ./conditional.sh
Enter a number: 80
80 is greater than 50
If..elif..else Statements
In some cases, you may want to test multiple conditions in one block. To implement such logic, we can use the if..elif..else statement.
In an if..elif..else statement, we add multiple conditional expressions where only one condition evaluates to true. If the first condition is true, execute its block; if not, check the second and continue until all the conditions are checked.
The syntax for the if..elif block is:
if condition1
then
// block1
elif condition2
then
// block2
elif conditionN
then
// blockN
else
// if all conditions evaluate to false
fi
The conditions are evaluated sequentially. If one evaluates to true, its code block is executed. However, if neither of the conditions is true, the else block is executed.
The else block is optional but recommended to handle a situation where no condition is true.
Here is an example script:
then
echo "Number = $num"
elif [[ $num -eq 20 ]]
then
echo "Number = $num"
elif [[ $num -eq 30 ]]
then
echo "Number = $num"
else
echo "Number is neither 10, 20 nor 30"
echo "Number = $num"
fi
The above script implements an elif block if the number entered is either 10, 20, or 30. If not, the else blocks to handle the condition as shown in the execution below:
Enter a number: 10
Number = 10
Enter a number: 20
Number = 20
Enter a number: 30
Number = 30
Enter a number: 51
Number is neither 10, 20 nor 30
Number = 51
You can have as many elif statements as you see fit. However, if such scenarios occur, consider implementing a case block.
Conditional Operators
In the examples above, we implemented conditional operators such as -eq (equal to) and -gt (greater than). ZSH supports other conditional operators such as:
- a -eq b – True if a is numerically equal to b
- a -gt b – True if a is numerically greater than b
- a -lt b – True if a is numerically less than b
- a -ne b – True is a is not numerically equal to b
- a -le b – True if a is numerically less than or equal to b
- a -ge b – True if a is numerically greater than or equal to b
- a != b – True if the string a is not equal to string b
- a = b – True if string a is equal to string b
- -z STRING – True if the length of the string is zero
- -n STRING – True if the length of the string is non-zero
- -a FILE – True if the file exists
- -h FILE – True if the file exists and is a symbolic link
- -f FILE – True if the file exists and is a regular file (not a directory or special file)
- -d FILE – True if the file exists and is a directory
- -e FILE – True if the file exists regardless of the type
- -r FILE – True if the file exists and is readable by the current process
- -w FILE – True if the file exists and is writeable by the current process
- -x FILE – True if the file exists and is executable by the current process
- -g FILE – True if file exists and has setgid bit set
- -s FILE – True if file exists and size is greater than zero
- -u – True if file exists and has setuid bit set
- -o FILE – True if file exists and is owned by the current ID
- ! EXP – True if the expression is false
- a && b – True if both a and b are true
- a || b – True if either a or b is true
The above are examples of conditional expressions you can use in your ZSH scripts. Check out the documentation to learn more.
Conclusion
This tutorial has shown you how to implement conditional logic in our ZSH scripts using if statements. Feel free to check out the ZSH scripting documentation to learn more.
Thank you for reading!