4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )

by Sasikala on June 21, 2010

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:

  1. if..then..fi statement (Simple If)
  2. if..then..else..fi statement (If-Else)
  3. if..elif..else..fi statement (Else If ladder)
  4. 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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 9 comments… read them below or add one }

1 Chris F.A. Johnson June 21, 2010 at 3:47 pm

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

2 Dennis Dashkevich July 14, 2012 at 11:57 am

Chris F.A. Johnson, you must have mistyped in the last line. There should be ‘fi’.

3 phil December 12, 2012 at 10:41 am

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 :)

4 Mike March 10, 2013 at 5:04 pm

Thanks for making this tutorial :-)

Again I echo Phil’s advice though to replace ‘If’ with ‘if’ as it causes an error.

Cheers
Mike

5 Meki February 18, 2014 at 9:29 pm

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

6 Chris F.A. Johnson March 9, 2014 at 10:27 am

Fix this line:

if ["shoose" -eq "1"] ; then

It should be:

if [ "$choose" = 1 ]; then

7 Akshay April 1, 2014 at 5:20 am

can i use if condition with cat command,,like
if [cat /etc/group|grep ]

8 Chris F.A. Johnson April 2, 2014 at 1:59 am

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

9 DrumBasstard November 28, 2014 at 10:18 pm

Hi, this tutorial is perfect for me
But I have a question

How to using if [$var -eq "string" ] ?

Leave a Comment

Previous post:

Next post: