Guide to ‘Bash if else’ with Examples

In the realm of programming, decision-making is a fundamental concept. It’s the backbone of logic in any code, and Bash scripting is no exception. This guide will delve into the intricacies of the Bash if else statement, providing you with a comprehensive understanding of its usage, along with a plethora of examples to solidify your knowledge.

Grasping the Basics of Bash ‘if’ Statement

The most basic form of decision-making in Bash scripting is the if statement. It follows this structure:

if TEST-COMMAND
then
  STATEMENTS
fi

The if statement begins with the if keyword, followed by the conditional expression, and then the then keyword. The statement concludes with the fi keyword. If the TEST-COMMAND evaluates to True, the STATEMENTS get executed. If TEST-COMMAND returns False, the STATEMENTS are ignored.

For instance, consider the following script that checks whether a given number is greater than 10:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
fi

When you run this script, it prompts you to enter a number. If you enter 15, the test command evaluates to true because 15 is greater than 10, and the echo command inside the then clause gets executed.

Unpacking the ‘if else’ Statement in Bash

The Bash if else statement allows you to define an alternative set of statements that get executed when the TEST-COMMAND evaluates to False. The structure is as follows:

if TEST-COMMAND
then
  STATEMENTS1
else
  STATEMENTS2
fi

If the TEST-COMMAND evaluates to True, STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, STATEMENTS2 will be executed.

Let’s modify the previous script to include an else clause:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
else
  echo "The variable is equal or less than 10."
fi

Now, if you run the script and enter a number, it will print a different message based on whether the number is greater or less/equal to 10.

Diving into ‘else if’ in Bash Scripting

The Bash if..elif..else statement allows you to specify additional conditions to check if the initial if condition is False. The structure is as follows:

if TEST-COMMAND1
then
  STATEMENTS1
elif TEST-COMMAND2
then
  STATEMENTS2
else
  STATEMENTS3
fi

If TEST-COMMAND1 evaluates to True, STATEMENTS1 will be executed. If TEST-COMMAND2 evaluates to True, STATEMENTS2 will be executed. If none of the test commands evaluate to True, STATEMENTS3 is executed.

Let’s add an elif clause to the previous script:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]]
then
  echo "The variable is equal to 10."
else
  echo "The variable is less than 10."
fi

Understanding Nested ‘if’ Statements in Bash

Bash allows you to nest if statements within if statements. You can place multiple if statements inside another if statement. Let’s consider a script that prompts you to enter three numbers and prints the largest number among the three:

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]]
then
  if [[ $VAR1 -ge $VAR3 ]]
  then
    echo "$VAR1 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
else
  if [[ $VAR2 -ge $VAR3 ]]
  then
    echo "$VAR2 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
fi

Handling Multiple Conditions with Bash ‘if’ Statements

The logical OR (||) and AND (&&) operators allow you to use multiple conditions in the if statements. Here’s a version of the previous script that uses the logical AND (&&) operator instead of nested if statements:

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]] && [[ $VAR1 -ge $VAR3 ]]
then
  echo "$VAR1 is the largest number."
elif [[ $VAR2 -ge $VAR1 ]] && [[ $VAR2 -ge $VAR3 ]]
then
  echo "$VAR2 is the largest number."
else
  echo "$VAR3 is the largest number."
fi

Wrapping Up: Mastering ‘if else’ in Bash

The if, if else, and if elif else statements in Bash scripting are powerful tools for controlling the flow of your script’s execution based on specific conditions. With a solid understanding of these concepts and plenty of practice, you’ll be well-equipped to write efficient and effective Bash scripts.