What Is Fi in Bash Scripting?

//

Larry Thompson

In Bash scripting, the fi command plays a crucial role in controlling the flow of execution. It is used to mark the end of an if statement or a case statement in a Bash script.

Understanding Conditional Statements in Bash

In order to understand the significance of the fi command, it is important to grasp the concept of conditional statements in Bash scripting.

Bash provides various conditional statements such as if-else, case, and for, which allow you to control the flow of your script based on certain conditions.

The if-else Statement:

The if-else statement is widely used in Bash scripting to perform actions based on specific conditions. It follows this basic structure:


if condition
then
    # code block executed when condition is true
else
    # code block executed when condition is false
fi

The keyword ‘if’ marks the beginning of an if-else statement, followed by the condition that needs to be evaluated. If the condition evaluates to true, the code block within the ‘then’ section is executed. If the condition evaluates to false, the code block within the ‘else’ section is executed.

The case Statement:

The case statement allows you to compare a variable or an expression against multiple patterns and execute code blocks based on matching patterns. It has a similar structure as follows:


case expression in
pattern1)
    # code block executed when expression matches pattern1
    ;;
pattern2)
    # code block executed when expression matches pattern2
    ;;
*)
    # code block executed when expression doesn't match any pattern
    ;;
esac

The expression is compared against different patterns, and the code block corresponding to the matching pattern is executed. The ‘*)’ represents the default case when none of the patterns match.

The Role of ‘fi’ in Bash Scripting

In both if-else and case statements, the fi keyword marks the end of the conditional block. It signifies that all the code blocks related to a specific condition have been executed and now it’s time to move on to the next block of code.

Example 1: if-else Statement

Consider this example:


#!/bin/bash

number=42

if [ $number -eq 42 ]
then
    echo "The number is equal to 42."
else
    echo "The number is not equal to 42."
fi

In this example, we check if the variable ‘number’ is equal to 42. If it is, we display a message saying “The number is equal to 42.”

Otherwise, we display a message saying “The number is not equal to 42. “

The fi command at the end of this script marks the end of our if-else statement.

Example 2: case Statement

Let’s take another example using a case statement:

fruit=”apple”

case $fruit in
apple)
echo “It’s an apple.” ;;
banana)
echo “It’s a banana.”

;;
*)
echo “It’s neither an apple nor a banana.” ;;
esac

In this example, we compare the value of the variable ‘fruit’ against different patterns. If it matches ‘apple’, we display “It’s an apple.”

If it matches ‘banana’, we display “It’s a banana.” If it doesn’t match any pattern, we display “It’s neither an apple nor a banana. “

Once again, the fi command denotes the end of our case statement.

Conclusion

The fi command is a crucial element in Bash scripting that marks the end of an if-else or case statement. It helps in organizing and controlling the flow of execution in your script based on specific conditions.

By understanding how to use fi, you can create more robust and efficient Bash scripts.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy