You know that if-else
condition is important for any programmer, regardless of their domain. To fully understand if-else in shell scripts, we need to understand the working of conditional programming.
Conditional programming is an important part of any programming language because sometimes there is code in our program we don’t necessarily want to execute, or we want to execute under certain conditions.
This is when the if-else in shell scripts comes in place.
In this article, you will learn how to use if-else conditions with bash scripting.
How to use if-else in shell script
Sometimes you may think you understand how things work just by seeing the syntax. But it’s easy it’s always a good choice to understand a function through example, so they can help you to actually reinforce your understanding.
Below you will find simple examples of if-else in shell scripts to give you a better idea of how it works.
Command | Description |
---|---|
&& | Logical AND |
$0 | Argument 0 i.e. the command that’s used to run the script |
$1 | First argument (change number to access further arguments) |
-eq | Equality check |
-ne | Inequality check |
-lt | Less Than |
-le | Less Than or Equal |
-gt | Greater Than |
-ge | Greater Than or Equal |
Using if-else for detecting odd and even
If the following example, we will check if a number is even or odd. To do so, we will use the modulus operator to check whether a number is a multiple of 2.
#!/bin/bash
number=14
if [ $((number % 2)) == 0 ]
then
echo "The number is even"
else
echo "The number is odd"
fi
Output
➜ The number is even
As you can see, we’ve enclosed a part of the condition within double brackets. That’s because we want the modulus operation to be performed before the condition is checked. Also, enclosing in double brackets runs statements in C-style, allowing you to process some C-style commands within bash scripts.
Essentially it tells your terminal that when you run the script it should use
bash
to execute it. It can be vital since you may be using a different shell on your machine (zsh , fish , sh , etc...), but you designed the script to work specifically with bash.
Using if-else to compare two numbers
Another common use case of if-else in shell scripts is for comparing two values. In the example below, we will see how you can achieve that.
#!/bin/bash
number1=5
number2=24
if [ $number1 -eq $number2 ]
then
echo "These 2 numbers are equals"
else
echo "These 2 numbers are differents"
fi
output
➜ These 2 numbers are different
Using if-else for password identification
Sometimes we have some use cases, where we need to verify the user prompt. So in our case, we will use the if-else to check whether the user password is correct or not.
#!/bin/bash
echo "Please enter your password"
read user_password
if [ $user_password = "lameAssPassword" ]
then
echo "The password is correct 🎉"
else
echo "The password is not correct... Goodbye 👋"
fi
output
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Conclusion
The function of if-else in shell script is an important asset for shell programmers. It is the best tool to use when you need to execute a set of statements based on pre-defined conditions.
Also note that all those examples above, have to be written in a file with extension .sh
and then to run them, you will execute this command ./your_file_name.sh
in your terminal.