Bash scripting provides conditional expressions to execute different code based on specified conditions.

Bash Shell Conditional Statements

At times, you may need to execute diverse code blocks depending on various decisions based on a given condition.

Bash scripting facilitates this through conditional statements

  if condition; then
     # true code
  elif another_condition; then
     # condition is false, and another_condition is true
  else
     # none of the above conditions are true
  fi
  • The if statement is utilized to execute a code block if a condition is true, with the syntax if then fi.
  • The else statement is employed to execute code if a condition is false, following the syntax if then else fi.
  • The if..elif..else statement comes in handy when you need to execute code if none of the preceding conditions are true. The syntax is as follows:

Notes:

  • A condition is an expression that evaluates to true or false in shell scripting.
  • A space is required before and after [ and ].
  • A semicolon before then is required.
  • if, else, then, elif, fi are reserved words in Bash.
  • A condition is an expression with a command.
    • A command containing single brackets syntax, the syntax example [expression] and is used for file string operations.
    • Double bracket syntax, example is [[expression]], which is employed for combining multiple conditions and handling regex patterns.
    • Double parentheses, syntax example is ((expression)), used for arithmetic operations.

If Conditional Statements

The if statement in Bash is used to execute a code block when a specified condition is true.

if [ condition ]; then
   # Execute code block if the condition is true
fi

In the above syntax:

  • Replace [ condition ] with the conditional expression.
  • The code block within the if statement is executed only if the specified condition evaluates to true.
  • Every if statement must conclude with fi.

Example

age=10
if [ $age -lt 50 ]; then
   echo "$age is less than 50"
fi

Output

10 is less than 50

If-Else Conditional Statements

The if-else conditional statements in Bash allow you to execute different code blocks depending on whether a condition is true or false.

if [ condition ]; then
   # Execute code block if the condition is true
else
   # Execute code block if the condition is false
fi

In the above syntax:

  • Replace [ condition ] with the expression to test.
  • The code block within the if statement is executed if the specified condition is true.
  • The code block within the else statement is executed if the condition is false.
  • Every if-else statement must conclude with fi.
#! /bin/sh
age=25
if [[ $age -gt 60 ]]; then
     echo "Senior Citizen"
else
     echo "Not Senior Citizen"
fi

In this example, if the age is greater than 60, it outputs “Senior Citizen”; otherwise, it outputs “Not Senior Citizen.”

If..Elif..Else Statements

Use if..elif..else conditional statements in Bash to execute different code blocks based on multiple conditions.

if [ condition1 ]; then
	# Execute code if condition1 is true
elif [ condition2 ]; then
	# Execute code if condition1 is false and condition2 is true
else
	# Execute code if both condition1 and condition2 are false
fi
  • The code block within the first if statement is executed if condition1 is true.
  • The code block within the first elif statement is executed if condition1 is false and condition2 is true``.
  • The else block is executed if both condition1 and condition2 are false.
  • Every if..elif..else statement must conclude with fi.

Example

age=25
if [[ $age -gt 60 ]]; then
    echo "Senior Citizen"
elif [[ $age -lt 14 ]]; then
    echo "Child"
else
    echo "Adult"
fi

In this example, the script checks if the age is greater than 60, less than 14, or falls into neither category, and it outputs the corresponding message.