Bash Else If – elif

Bash Else If

In this Bash Tutorial, we shall learn the syntax and usage of Bash Else If statement with example Bash Scripts.

Bash Else If is kind of an extension to Bash If Else statement.

Syntax of Bash Else IF – elif

Syntax of Else If statement in Bash Shell Scripting is as shown below :


<expression>Similar to Bash If statement, you may provide a single condition or multiple conditions after if keyword.
<commands>Set of commands to be run when the <condition> is true.
elifElse If


Example 1 : Else If (elif)

In this example, we shall look into two scenarios where in one elif expression, condition is true and in the other, condition is false.

Bash Script File

In the first if expression, condition is false, so bash evaluates the expression for elif block. As the expression is true, the commands in the elif (else if) block are executed.


Example 2 : Bash Else If – Multiple Conditions

In this example, we shall look into a scenario where there could be multiple conditions for elif (else if) expression.

Bash Script File

Note : Else block is optional.



In this tutorial, we have learnt about the syntax and usage of Bash Else IF statement with example bash scripts.