In this Bash Tutorial, we will learn the syntax and usage of Bash Else If statement with example Bash scripts. The elif keyword is used when a shell script has to test more than one condition in order, instead of writing many separate if statements.

Bash Else If elif Statement

Bash Else If is kind of an extension to Bash If Else statement. In Bash else-if, there can be multiple elif blocks with a boolean expression for each of them. The Bash elif ladder is useful when exactly one matching branch should run from a group of possible conditions.

The word elif means “else if”. Bash checks the first if condition, then checks each elif condition only when the earlier conditions are false. Once a true condition is found, Bash runs that block and skips the remaining elif and else blocks.

Syntax of Bash Else IF – elif

Following is the syntax of Else If statement in Bash Shell Scripting.

</>
Copy
if <expression>; then
	<commands>
elif <expression>; then
	<commands>
else
	<commands>
fi

where

<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

In this if-else-if ladder, the expressions are evaluated from top to bottom. Whenever an expression is evaluated to true, corresponding block of statements are executed and the control comes out of this if-else-if ladder.

If none of the expressions evaluate to true, the block of statements inside else block is executed. Also, note that the else block is optional.

Whenever a default action has to be performed when none of the if and else-if blocks get executed, define else block with the default action.

Bash Else If

How Bash Evaluates an elif Ladder

A Bash elif ladder should be written from the most specific condition to the most general condition. This prevents a broad condition from matching first and stopping Bash before it reaches a more precise check.

  • Bash starts with the condition after if.
  • If the if condition is true, Bash runs that block and jumps to the command after fi.
  • If the if condition is false, Bash tests the first elif condition.
  • Bash continues this process until one condition is true.
  • If no condition is true, Bash runs the optional else block.

The then keyword is required after every if and elif condition. The whole conditional statement must end with fi.

Example 1: Basic Bash Else If (elif)

In this example, we will look into two scenarios wherein one elif expression the condition evaluates to true and in the other to false.

Bash Script File

</>
Copy
#!/bin/bash

n=2

if [ $n -eq 1 ]; then
	echo value of n is 1
elif [ $n -eq 2 ]; then
	echo value of n is 2
else
	echo value of n is other than 1 and 2
fi

Output

~/workspace/bash$ ./bash-elif-example 
value of n is 2

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 with 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

</>
Copy
#!/bin/bash

n=2

if [ $n -eq 1 ]; then
	echo value of n is 1
elif [[ $n -eq 2 && $n -lt 5 ]]; then
	echo value of n is less than threshold
fi

Note : Else block is optional.

Output

~/workspace/bash$ ./bash-elif-example
value of n is less than threshold

Bash elif Example with String Conditions

The elif statement is not limited to numbers. You can also compare strings. In Bash, it is safer to quote variables inside test expressions so that empty values or values containing spaces do not break the condition.

</>
Copy
#!/bin/bash

role="editor"

if [ "$role" = "admin" ]; then
    echo "Full access"
elif [ "$role" = "editor" ]; then
    echo "Edit access"
elif [ "$role" = "viewer" ]; then
    echo "Read-only access"
else
    echo "Unknown role"
fi

Output

Edit access

Here, the first condition is false because role is not admin. The next elif condition is true, so Bash prints Edit access and skips the remaining branches.

Bash elif Example with File Test Conditions

A common use of Bash elif is to check file system conditions. The following script checks whether a path is a regular file, a directory, or something else.

</>
Copy
#!/bin/bash

path="/tmp"

if [ -f "$path" ]; then
    echo "$path is a file"
elif [ -d "$path" ]; then
    echo "$path is a directory"
else
    echo "$path does not exist as a regular file or directory"
fi

Common file tests used with Bash elif

File testMeaning in a Bash condition
-f "$path"True when the path is a regular file.
-d "$path"True when the path is a directory.
-e "$path"True when the path exists.
-r "$path"True when the path is readable.
-w "$path"True when the path is writable.
-x "$path"True when the path is executable or searchable.

Using [ ] and [[ ]] with Bash elif Conditions

Bash supports both the single bracket test command [ ] and the extended conditional expression [[ ]]. Many simple scripts use [ ], while [[ ]] is often preferred in Bash-specific scripts because it handles patterns and compound expressions more comfortably.

Condition formTypical use in elifExample
[ ]Portable test expressions for numbers, strings, and files.elif [ "$n" -eq 2 ]; then
[[ ]]Bash-specific conditions, compound tests, and pattern matching.elif [[ "$name" == a* ]]; then

When you use [ ], keep spaces after [ and before ]. For example, [ "$n" -eq 2 ] is valid, but [$n -eq 2] is not valid Bash syntax.

Bash elif Operators for Numbers and Strings

The condition inside elif depends on the type of value you are checking. Use numeric operators for integer comparison and string operators for text comparison.

OperatorUse withMeaningExample condition
-eqNumbersEqual to[ "$n" -eq 10 ]
-neNumbersNot equal to[ "$n" -ne 10 ]
-ltNumbersLess than[ "$n" -lt 10 ]
-leNumbersLess than or equal to[ "$n" -le 10 ]
-gtNumbersGreater than[ "$n" -gt 10 ]
-geNumbersGreater than or equal to[ "$n" -ge 10 ]
=StringsEqual to[ "$name" = "Tom" ]
!=StringsNot equal to[ "$name" != "Tom" ]
-zStringsString is empty[ -z "$name" ]
-nStringsString is not empty[ -n "$name" ]

Common Bash elif Syntax Mistakes

Most Bash elif errors come from small syntax issues. Check these points when a script prints errors such as syntax error near unexpected token or does not enter the expected branch.

  • Use elif, not else if. Bash does not use two separate words for this branch.
  • Add then after every if and elif condition.
  • Close the full conditional block with fi.
  • Keep spaces inside test brackets: [ "$n" -eq 2 ].
  • Quote variables in string and file tests: [ "$name" = "admin" ].
  • Use numeric comparison operators such as -eq and -gt for integers, not =.
  • Place the optional else block after all elif blocks.

For Bash-specific conditional behavior, you may also refer to the Bash manual section on conditional constructs.

When to Use Bash elif Instead of Separate if Statements

Use a Bash elif ladder when the conditions are related and only one branch should execute. For example, checking a status code, user role, file type, or numeric range usually fits an if, elif, else structure.

Use separate if statements when more than one condition may be true and each true condition should run its own commands. This distinction is important because an elif ladder stops after the first true condition.

</>
Copy
#!/bin/bash

score=72

if [ "$score" -ge 90 ]; then
    echo "Grade A"
elif [ "$score" -ge 75 ]; then
    echo "Grade B"
elif [ "$score" -ge 60 ]; then
    echo "Grade C"
else
    echo "Needs improvement"
fi

In this script, a score of 72 matches the score -ge 60 condition. Bash prints only one grade because the conditions are part of the same elif ladder.

QA Checklist for a Bash Else If Tutorial

  • Does every Bash elif example include the required then and closing fi?
  • Are numeric comparisons shown with operators such as -eq, -lt, and -gt?
  • Are string and file path variables quoted in test expressions?
  • Does the explanation clearly say that only the first true branch in an elif ladder runs?
  • Are [ ] and [[ ]] examples described without mixing portable shell syntax and Bash-specific syntax incorrectly?
  • Does the tutorial include at least one default else branch example and one optional-else example?

FAQs on Bash Else If elif

What is if else and elif in Bash?

In Bash, if starts a conditional block, elif adds another condition to check when earlier conditions are false, and else defines the default block to run when none of the earlier conditions are true.

Can Bash have multiple elif blocks?

Yes. A Bash script can have multiple elif blocks. Bash evaluates them from top to bottom and runs the first block whose condition is true.

Is the else block required in a Bash elif statement?

No. The else block is optional. Add it only when you need a default action for cases where the if and all elif conditions are false.

Should I use else if or elif in Bash?

Use elif in Bash. Writing else if is not the normal Bash else-if syntax and often causes incorrect nesting or syntax errors.

What is the difference between [ ] and [[ ]] in Bash elif conditions?

[ ] is the traditional test command and is suitable for many simple checks. [[ ]] is a Bash keyword with extended conditional features, such as easier compound expressions and pattern matching. Use [[ ]] when your script is specifically written for Bash.

Summary of Bash Else If elif Usage

Concluding this Bash Tutorial, we have learned about the syntax and usage of Bash Else IF statement with example bash scripts. The key point is that Bash checks an if, elif, else ladder in order and runs only the first matching branch. Use elif for related conditions where one result should be selected from several possible choices.