Else-If Statement in Bash

In the realm of programming, decision-making is a fundamental aspect of crafting effective code. Bash, a widely used Unix shell scripting language, provides a powerful tool for making decisions based on specific conditions: the else-if statement. This statement is used to handle multiple conditions in a loop that cannot be handled by the if-else statement. So, if you want to run several conditions at a time in a bash script, using else-if is the way to go.

In this article, I will provide you with a comprehensive overview of bash else-if conditional statements. I will also show you how to use the else-if statement in the bash script.

What is Else-If Statement in Bash?

In bash, the else-if (elif) is a decision-making statement that controls the execution of statements based on conditions that contain a boolean expression “true” or “false”. It works like if the first condition isn’t true, the second condition in the elif block is checked and executed, and when all the conditions are false, the else block is executed. You can have multiple elif conditions in one code.

Syntax of Else-If (elif) Statement

Here’s the basic syntax of the elif statement:

if [ condition1 ]; then
    # Code block executed if condition1 is true
elif [ condition2 ]; then
    # Code block executed if condition2 is true
    # Code block executed if none of the conditions are true

If condition1 is true, then the script executes the if block. When condition2 is true, then it prints the elif block. If both condition1 and condition2 are false, then the else block is executed.

6 Examples to Use Else-If Statement in Bash

Using the else-if (elif) conditional statement in bash scripting is a great way to handle many different conditions in a well-structured and easy-to-understand way. This makes your scripts more versatile and able to maintain different situations. Here are 6 examples to use else-if in bash:

1. Check if Number is Positive or Negative

To Check if a number is positive or negative, you can use an elif statement. For that, the -gt option is used inside the if block to indicate that it’s a positive number when the input number is greater than zero. Conversely, the -lt option inside the elif checks whether the number is less than zero indicating the number is negative. Here’s how:

read -p "Enter a number: " num

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

The if statement checks whether the number is greater than 0. It executes “The number is positive” if the condition is true. But if it is false, the elif statement checks whether the number is less than 0, and prints “The number is negative”. If both conditions are false, the else block is executed, printing “The number is zero.”.

checking if a number is positive or negative using else ifIn this picture, you can see that number 2 is positive as it is greater than 0 and number -1 is negative as it is less than zero.

2. Check if File is Regular File or Directory

To check whether a file is a regular file or directory or none of these, using a single code, use the -f flag inside the if condition to check if it is a regular file, and the -d flag inside the elif condition to check if the file is a directory. Let’s look at the following bash script to see how it works:

read -p "Enter a filename: " filename

if [ -f "$filename" ]; then
    echo "It's a regular file."
elif [ -d "$filename" ]; then
    echo "It's a directory."
    echo "It's neither a file nor a directory."

Here, the read command is used to take user input. The -p option displays a prompt, and the entered value is stored in the variable filename. Then the elif statements check whether it’s a regular file or a directory. If neither condition is true, it outputs that it’s neither a file nor a directory.

checking if a file is a regular file or directory using else if in bashAs you can see it shows that it’s a directory because Downloads is a directory, not a regular file.

3. Verify Username Using Elif

To verify the username using the elif statement, you can use the equal to == operator within the condition block. If the user name is equal to the right user name, only then you will get access. As an elif structure let’s check multiple conditions, you can even check multiple users at the same time. Here is a bash script to check for 2 usernames:

read -p "Enter your username: " username

if [ "$username" == "mou" ]; then
    echo "Welcome, mou!"
elif [ "$username" == "john" ]; then
    echo "Welcome, john!"
    echo "Invalid username."

Here, the if statement checks if the entered username is equal to “mou.” and prints  “Welcome, mou!” if it is true. The else-if statement checks if the entered username is equal to “john” and prints “Welcome, john!” if it is true. If the entered username is not “mou” or “john,” it prints an “Invalid username” message.

verifying username with else if statement in bash

When I signed in with the username “mou” and “john”, I got the welcome message printed out. But when I signed in with another username “mitu”, it showed me an “invalid username” as it is not a valid username on my system.

4. Check if String Contains Specific Numbered Characters

To check if a string contains specific numbered characters in bash, use "${#string}" syntax inside the else-if conditions which check the number of characters present in the strings including spaces. Here, the -eq option verifies whether the string has 3 characters, 5 characters, or a different number of characters. Let’s see the detailed bash script:

read -p "Enter a string: " string

if [ "${#string}" -eq 3 ]; then
    echo "The string has three characters."
elif [ "${#string}" -eq 5 ]; then
    echo "The string has five characters."
    echo "The string has a different number of characters."

In this script, the if condition checks whether the length of the string is equal to 3. If so, it executes “The string has three characters.”. The elif condition checks if the string length is equal to 5. It executes “The string has five characters.” if the condition is true. Otherwise, it prints the else block message “The string has a different number of characters.”.

checking if a string contains specific characters using else if

In this image, we can see that when the string “hello” is entered, it indicates that the string contains 5 characters. However, when I entered the string “hello world” (which does not contain 3 or 5 characters), it showed that the string contains different characters. Moreover, when I entered “mou”, which is a string with 3 characters, it showed that “it has 3 characters”.

5. Check Leap Year Using Three Conditions

You can use more than two conditions with an else-if structure. In this example, I will show you how to check whether a particular year is a leap year.

Basically, a leap year is a year that has 366 days instead of the usual 365 days. There are three conditions that must be met for a year to be a leap year: the year must be divisible by 4, the year must not be divisible by 100, and the year must be divisible by 400. Here, the modulo (%) is used to calculate the remainder of the divisions. Let’s see a full bash script:

read -p "Enter a year: " year

if [ "$((year % 400))" -eq 0 ]; then
    echo "$year is a leap year."
elif [ "$((year % 100))" -eq 0 ]; then
    echo "$year is not a leap year."
elif [ "$((year % 4))" -eq 0 ]; then
    echo "$year is a leap year."
    echo "$year is not a leap year."

The first if statement checks if the year is divisible by 400. If it is, then the year is a leap year and the script prints a message to the console. The second elif statement checks if the year is divisible by 100. If so, then the script prints “$year is not a leap year.”. The third elif statement checks if the year is divisible by 4. If it is, then the year is a leap year and the script executes the corresponding code block. The else block is used if none of the conditions are true. In this case, the script prints “$year is not a leap year.”.

checking if a year is leap year using else if statement

Since the year 2023 is not divisible by 400, 100, and 4, it displays else block code “2023 is not a leap year”.

6. Check Days of Week With Multiple Conditions

If you require to execute multiple conditions within a loop, you can use the elif statement. Here’s a simple example to check a day of the week using elif expression. The -eq option is used here to check the equality between variables:

read -p "Enter a day (1-7): " day

if [ "$day" -eq 1 ]; then
    echo "It's Monday."
elif [ "$day" -eq 2 ]; then
    echo "It's Tuesday."
elif [ "$day" -eq 3 ]; then
    echo "It's Wednesday."
elif [ "$day" -eq 4 ]; then
    echo "It's Thursday."
elif [ "$day" -eq 5 ]; then
    echo "It's Friday."
elif [ "$day" -eq 6 ]; then
    echo "It's Saturday."
elif [ "$day" -eq 7 ]; then
    echo "It's Sunday."
    echo "Invalid day."

The -eq operator checks whether the input value is equal to 1, then it prints it’s a Monday which is the first day of the week. Similarly, if you enter value 2, it will print it’s a Tuesday which is the second day of the week. Similarly, it prints the other days of the week. However, if you put any number except 1 to 7 then it executes “Invalid day.”.

checking a day of the week using else if

After entering the number 7, it will display that it is Sunday, which is the seventh day of the week. If you enter the number 1, it will execute Monday (the first day of the week). However, if you enter the number 8, which does not represent any day in the week, it will print ” Invalid day”.


In conclusion, I have presented an in-depth analysis of the ‘else-if’ conditional statement in bash. By now, you are aware of the concept of ‘else-if’ statements in bash and the significance of using them in bash scripts. Furthermore, I have presented 6 practical ‘else-if’ statements to help you gain a better understanding of the concept. Thank you for reading.

People Also Ask

What is the purpose of the else-if statement in Bash?

The else-if statement, often written as elif in Bash, allows you to test multiple conditions in a sequential manner. It provides an alternative block of code to be executed if the preceding if statement evaluates to false.

Are else-if and elif interchangeable?

Yes, else-if and elif are interchangeable in Bash. Both keywords serve the same purpose in creating conditional branches.

What is the difference between an else-if statement and an if-else statement?

An else-if statement allows you to evaluate multiple conditions sequentially, while an if-else statement only allows you to evaluate one condition. An else-if statement is more versatile than an if-else statement because it allows you to handle more complex decision-making scenarios.

Can I have multiple elif statements in a single if block?

Yes, definitely. You can have multiple elif statements to test additional conditions. The code inside the first true condition or the else block will be executed.

Are the conditions always enclosed in square brackets “[ ]”?

Yes, in Bash, the conditions must be enclosed in square brackets “[]”. The spaces around the square brackets are crucial for proper syntax.

Can I use else-if without an initial if statement?

No, else-if (elif) is always used in conjunction with an initial if statement. It follows the initial if block to test additional conditions if the initial condition is false.

Can I use logical operators with elif conditions?

Yes, you can use logical operators like -a (and) and -o (or) to combine multiple conditions within the square brackets.

Related Articles

<< Go Back to Bash Conditional Statements | Bash Scripting Tutorial

5/5 - (6 votes)
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment