Skip to content

08. Bash if, else, and fi Statements

Overview

Bash if, else, and fi statements are used to make decisions in a shell script. They allow a script to execute different commands depending on whether a condition is true or false.

Basic Syntax

if [ condition ]; then
    # commands if condition is true
else
    # commands if condition is false
fi

The if statement checks the condition.

The then keyword marks the commands that should execute when the condition is true.

The else block executes when the condition is false.

The fi keyword ends the if statement.

A space is required after [ and before ].

if [ condition ]; then

Incorrect:

if [condition]; then

Checking if a Directory Exists

The -d operator can be used to check whether a directory exists.

#!/bin/bash

directory="./BashScripting"

if [ -d $directory ]; then
    echo "Directory exists"
else
    echo "Directory does not exist"
fi

Explanation

directory="./BashScripting"

Stores the directory path in the directory variable.

if [ -d $directory ]; then

Checks whether the specified path is an existing directory.

The -d test returns true when the directory exists.

echo "Directory exists"

Runs when the directory exists.

else
    echo "Directory does not exist"

Runs when the directory does not exist.

fi

Marks the end of the if statement.

Example Output

When the directory does not exist:

$ ./bash_if_else.sh
Directory does not exist

Create the directory:

$ mkdir BashScripting

Run the script again:

$ ./bash_if_else.sh
Directory exists

Nested if/else

A nested if means placing one if statement inside another if statement.

Nested conditions can be used when a script needs to perform multiple levels of decision-making.

Example

#!/bin/bash

choice=4

echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "

while [ $choice -eq 4 ]; do

    read choice

    if [ $choice -eq 1 ]; then
        echo "You have chosen word: Bash"

    else
        if [ $choice -eq 2 ]; then
            echo "You have chosen word: Scripting"

        else
            if [ $choice -eq 3 ]; then
                echo "You have chosen word: Tutorial"

            else
                echo "Please make a choice between 1-3 !"
                echo "1. Bash"
                echo "2. Scripting"
                echo "3. Tutorial"
                echo -n "Please choose a word [1,2 or 3]? "
                choice=4
            fi
        fi
    fi

done

Explanation

The variable is initially set to 4:

choice=4

The value 4 is used to keep the while loop running until the user enters a valid choice.

The menu is displayed:

echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"

The script then asks the user for input:

read choice

The first condition checks whether the user selected 1:

if [ $choice -eq 1 ]; then
    echo "You have chosen word: Bash"

If the choice is not 1, another if statement checks for 2:

if [ $choice -eq 2 ]; then
    echo "You have chosen word: Scripting"

If the choice is not 2, another condition checks for 3:

if [ $choice -eq 3 ]; then
    echo "You have chosen word: Tutorial"

If none of the valid choices were entered, the script displays an error message and sets:

choice=4

This causes the while loop to continue and display the menu again.

while Loop with Nested if

The loop uses:

while [ $choice -eq 4 ]; do

This means the loop continues while choice is equal to 4.

When the user enters a valid value such as 1, 2, or 3, the value changes and the loop condition becomes false.

For example:

Please choose a word [1,2 or 3]? 5
Please make a choice between 1-3 !
1. Bash
2. Scripting
3. Tutorial
Please choose a word [1,2 or 3]? 2
You have chosen word: Scripting

The first input, 5, is invalid, so the script displays the menu again.

The second input, 2, is valid, so the script prints:

You have chosen word: Scripting

Common Bash Test Operators

Some commonly used test operators are:

Operator Purpose
-d Check if a directory exists
-f Check if a file exists
-e Check if a path exists
-r Check if a file is readable
-w Check if a file is writable
-x Check if a file is executable
-eq Equal
-ne Not equal
-gt Greater than
-lt Less than
-ge Greater than or equal
-le Less than or equal

Key Points

  • if is used to test a condition.

  • then starts the commands executed when the condition is true.

  • else handles the false condition.

  • fi ends the if statement.

  • Spaces are required inside [ ].

  • -d checks whether a directory exists.

  • Nested if statements allow multiple conditions to be checked.

  • A while loop can be combined with if/else to repeatedly process user input until a valid condition is met.