Bash Elif Statement


Bash Elif Statement

In Bash scripting, the elif statement allows you to make decisions based on multiple conditions. It executes commands based on whether any specified condition is true, moving to the next condition if the previous one is false.


Syntax

if [ condition1 ]; then
    # commands if condition1 is true
elif [ condition2 ]; then
    # commands if condition2 is true
else
    # commands if none of the above conditions are true
fi

The basic syntax involves using if followed by the first condition in square brackets, the commands to execute if the first condition is true, the elif statement followed by the second condition and its commands, and an optional else statement for commands if none of the conditions are true.


Example Bash Elif Statements

Let's look at some examples of how to use elif statements in Bash:

1. Elif Statement to Compare Numbers

This script checks if the variable number is equal to 5, greater than 5, or less than 5 and prints a corresponding message.

#!/bin/bash

number=7

if [ $number -eq 5 ]; then
    echo "The number is equal to 5."
elif [ $number -gt 5 ]; then
    echo "The number is greater than 5."
else
    echo "The number is less than 5."
fi

In this script, the variable number is assigned the value 7. The if statement first checks if number is equal to 5 using the -eq operator. If false, the elif statement checks if number is greater than 5 using the -gt operator. If both conditions are false, the else statement prints a message indicating the number is less than 5.

Elif statement to compare numbers in Bash

2. Elif Statement to Compare Strings

This script checks if the variable string is 'hello', 'world', or something else and prints a corresponding message.

#!/bin/bash

string="world"

if [ "$string" = "hello" ]; then
    echo "The string is 'hello'."
elif [ "$string" = "world" ]; then
    echo "The string is 'world'."
else
    echo "The string is something else."
fi

In this script, the variable string is assigned the value 'world'. The if statement first checks if string is equal to 'hello' using the = operator. If false, the elif statement checks if string is equal to 'world'. If both conditions are false, the else statement prints a message indicating the string is something else.

Elif statement to compare strings in Bash

3. Elif Statement to Check Multiple Conditions

This script checks multiple conditions for the variable number and prints a corresponding message based on whether the number is positive, negative, or zero.

#!/bin/bash

number=-3

if [ $number -gt 0 ]; then
    echo "The number is positive."
elif [ $number -lt 0 ]; then
    echo "The number is negative."
else
    echo "The number is zero."
fi

In this script, the variable number is assigned the value -3. The if statement first checks if number is greater than 0 using the -gt operator. If false, the elif statement checks if number is less than 0 using the -lt operator. If both conditions are false, the else statement prints a message indicating the number is zero.

Elif statement to check multiple conditions in Bash

Conclusion

The Bash elif statement is a crucial tool for performing multiple conditional operations in shell scripting. Understanding how to use elif statements can help you create more dynamic and responsive scripts.