Bash conditional statements perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. These statements are used to execute different parts of your shell program depending on whether certain conditions are true. The ability to branch makes shell scripts powerful.
In Bash, we have the following conditional statements:
- if..then..fi statement (Simple If)
- if..then..else..fi statement (If-Else)
- if..elif..else..fi statement (Else If ladder)
- if..then..else..if..then..fi..fi..(Nested if)
These are similar to the awk if statements we discussed earlier.
1. Bash If..then..fi statement
if [ conditional expression ] then statement1 statement2 . fi
This if statement is also called as simple if statement. If the given conditional expression is true, it enters and executes the statements enclosed between the keywords “then” and “fi”. If the given expression returns zero, then consequent statement list is executed.
if then fi example:
#!/bin/bash count=100 if [ $count -eq 100 ] then echo "Count is 100" fi
2. Bash If..then..else..fi statement
If [ conditional expression ] then statement1 statement2 . else statement3 statement4 . fi
If the conditional expression is true, it executes the statement1 and 2. If the conditional expression returns zero, it jumps to else part, and executes the statement3 and 4. After the execution of if/else part, execution resume with the consequent statements.
if then else fi example:
#!/bin/bash count=99 if [ $count -eq 100 ] then echo "Count is 100" else echo "Count is not 100" fi
Note: This article is part of the ongoing Bash Tutorial series.
3. Bash If..elif..else..fi
If [ conditional expression1 ] then statement1 statement2 . elif [ conditional expression2 ] then statement3 statement4 . . . else statement5 fi
You can use this if .. elif.. if , if you want to select one of many blocks of code to execute. It checks expression 1, if it is true executes statement 1,2. If expression1 is false, it checks expression2, and if all the expression is false, then it enters into else block and executes the statements in the else block.
if then elif then else fi example:
#!/bin/bash count=99 if [ $count -eq 100 ] then echo "Count is 100" elif [ $count -gt 100 ] then echo "Count is greater than 100" else echo "Count is less than 100" fi
4. Bash If..then..else..if..then..fi..fi..
If [ conditional expression1 ] then statement1 statement2 . else if [ conditional expression2 ] then statement3 . fi fi
If statement and else statement could be nested in bash. The keyword “fi” indicates the end of the inner if statement and all if statement should end with the keyword “fi”.
The “if then elif then else fi” example mentioned in above can be converted to the nested if as shown below.
#!/bin/bash count=99 if [ $count -eq 100 ] then echo "Count is 100" else if [ $count -gt 100 ] then echo "Count is greater than 100" else echo "Count is less than 100" fi fi
In our next article, we’ll discuss about how to use Bash conditional expressions with practical examples.
Recommended Reading
Bash 101 Hacks, by Ramesh Natarajan. I spend most of my time on Linux environment. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting. Based on my Bash experience, I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. If you’ve been thinking about mastering Bash, do yourself a favor and read this book, which will help you take control of your Bash command line and shell scripting.
Comments on this entry are closed.
The correct description of an if statement is:
if
then
….
can be any command or list of commands, not just test (a.k.a. ‘[‘ )
if grep date ~/.bashrc
then
:
fi
if
Chris F.A. Johnson, you must have mistyped in the last line. There should be ‘fi’.
Hey,
you should replace the “If” by “if” – I just tried the first one and spent some time figuring out that that was the problem. It will actually print a completely useless error message, so beginners may be kind of desperate 🙂
Thanks for making this tutorial 🙂
Again I echo Phil’s advice though to replace ‘If’ with ‘if’ as it causes an error.
Cheers
Mike
I tried to use this program to calculate for me but it did not work for me
echo “This program will calculate for you”
echo “1. Addition”
echo “2. Subtraction”
echo “3. Multiplication”
echo “Please shoose 1, 2 3 ”
read choose
if [“shoose” -eq “1”] ; then
echo “You choose number 1, you want to add numbers’
else
echo ” You choose other number”
fi
Thank you
Fix this line:
if [“shoose” -eq “1”] ; then
It should be:
if [ “$choose” = 1 ]; then
can i use if condition with cat command,,like
if [cat /etc/group|grep ]
The syntax is:
if command
then
….
“[” is a command
“cat” is a command
Use either one, not both.
if cat …
then
But *not* if cat /etc/group|grep … !!
Use:
if grep -q REGEX /etc/group
then
Hi, this tutorial is perfect for me
But I have a question
How to using if [$var -eq “string” ] ?
@DB
take care of the whitespaces if u use [ ] instead of “test”
for example: if [ $# -lt 2 ]; then
so after and before last bracket use a whitespace!
Hi,
Would like to know if we can determine the success of the statement?
#!/bin/bash
{My Task Here}
If Task was Successful =====> How is possible to report the result of above command.
Echo “Above command was successful”
Else
Echo ” Something get wrong, try again”
Item number 2. specifies ‘If’ with an upper-case ‘I’ this will not work.
Missing the ; after the conditional
Hi..
Having problems. Been years, since I did, this… & now, I cannot remember, squat! I’m trying to delete files that are only garbage:
code:
cd /foobar/foobar
if *.dsf then rm *.dsf
—
…& repeat for 2 more folders.
I’ve tried: #
!/bin/sh
#
cd foobar/foobar/foobar
if *.dsf then rm *.dsf
fi
and
if [ *.dsf ] then rm *.dsf
Nothing, works. Get a syntax error or unexpected end, depending upon which I use. At my wits’ end. Thx, if anyone sees this, & can help.
can you please explain me on writing a script which add user with uid, gid, comment from the given .txt file.
Thanks
giving error in if [“$IP” -eq 209.249.130] line. Please help me out