How to Write IF-Statements in a Bash-Script

Commands, syntax, and examples

With an if statement, which is a type of conditional statement, you can perform different actions depending on specified conditions. It effectively gives the system the ability to make decisions.

Bash basic If statement

Here's an example of the simplest form of an if statement:

count=5
if [ $count == 5 ]
then
echo "$count"
fi

In this example, the variable count specifies a condition that is used as part of the if statement. Before the if statement is executed, the variable count is assigned the value 5. The if statement then checks whether the value of count is 5. If that is the case, the statement between the keywords then and fi are executed. Otherwise, any statements following the if statement are executed.

The keyword fi is if spelled backward. The bash scripting language uses this convention to mark the end of a complex expression, such as an if statement or case statement.

The echo statement prints its argument, in this case, the value of the variable count, to the terminal window. Indentation of the code between the keywords of the if statement improves readability but isn't necessary.

Bash If statement with Else

If you have a situation where a piece of code should only be executed if a condition is not true, use the keyword else in an if statement, as in this example:

count=5
if [ $count == 5 ]
then
echo "$count"
else
echo "count is not 5"
fi

If the condition $count == 5 is true, the system prints the value of the variable count. Otherwise, it prints the string count is not 5.

Bash If statement with Elif

If you want to differentiate between multiple conditions, use the keyword elif, which is derived from else if, as in this example:

if [ $count == 5 ]
then
echo "count is five"
elif [ $count == 6 ]
then
echo "count is six"
else
echo "none of the above"
fi

If count is 5, the system prints count is five. If count isn't 5 but 6, the system prints count is six. If it is neither 5 nor 6, the system prints none of the above.

You can have any number of elif clauses. An example of multiple elif conditions is:

if [ $count == 5 ]
then
echo "count is five"
elif [ $count == 6 ]
then
echo "count is six"
elif [ $count == 7 ]
then
echo "count is seven"
elif [ $count == 8 ]
then
echo "count is eight"
elif [ $count == 9 ]
then
echo "count is nine"
else
echo "none of the above"
fi
Bash Case statement

A more compact way to write such statements with multiple conditions is the case method. It functions similarly to the if statement with multiple elif clauses but is more concise. For example, the above piece of code can be re-written with the case statement as follows:

case "$count" in
5)
echo "count is five"
;;
6)
echo "count is six"
;;
7)
echo "count is seven"
;;
8)
echo "count is eight"
;;
9)
echo "count is nine"
;;
*)
echo "none of the above"
esac
Bash If statement in While loop

if statements are often used inside for-loops or while-loops, as in this example:

count=1
done=0
while [ $count -le 9 ]
do
sleep 1
(( count++ ))
if [ $count == 5 ]
then
continue
fi
echo "$count"
done
echo Finished

You can also have nested if statements. The simplest nested if statement is of the form: if...then...else...if...then...fi...fi. However, an if statement can be nested with arbitrary complexity.

See also how to pass arguments to a bash script, which shows how to use conditionals to process parameters passed from the command line.

The bash shell provides other programming constructs, such as for-loops, while-loops, and arithmetic expressions.