Basic Conditional Statements in Bash

By Anurag Singh

Updated on Dec 25, 2024

Basic Conditional Statements in Bash

In this tutorial, we'll learn basic conditional statements in bash.

Conditional statements are a fundamental part of Bash scripting, enabling scripts to make decisions and execute different code paths based on specific conditions. This tutorial will explore the if, else, and elif syntax in Bash, as well as basic comparison and logical operators.

Prerequisites

Before getting started, ensure you have the following:

  • A KVM VPS or dedicated server with any Linux distro installed.
  • A non-root user with sudo privileges.
  • Basic knowledge of Linux commands.

Understanding if, else, and elif Syntax

The if statement is used to evaluate a condition. If the condition is true, the commands within the if block are executed. The else statement provides an alternative set of commands if the condition is false. The elif (short for "else if") statement allows for additional condition checks.

Syntax:

if [ condition ]; then
    # commands to execute if condition is true
elif [ another_condition ]; then
    # commands to execute if another_condition is true
else
    # commands to execute if no conditions are true
fi

The structure ends with fi, which signifies the end of the if block.

Example 1: Basic if Statement

#!/bin/bash

number=10

if [ $number -gt 5 ]; then
    echo "The number is greater than 5."
fi

Example 2: Adding else

#!/bin/bash

number=3

if [ $number -gt 5 ]; then
    echo "The number is greater than 5."
else
    echo "The number is 5 or less."
fi

Example 3: Using elif

#!/bin/bash

number=8

if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
elif [ $number -eq 8 ]; then
    echo "The number is exactly 8."
else
    echo "The number is less than or equal to 10 but not 8."
fi

Comparison Operators in Bash

Comparison operators allow you to compare values. They are commonly used within [ and ] brackets.

Integer Comparison Operators:

  • -eq: Equal to
  • -ne: Not equal t
  • -gt: Greater than
  • -ge: Greater than or equal to
  • -lt: Less than
  • -le: Less than or equal to

Example:

#!/bin/bash

number=15

if [ $number -eq 15 ]; then
    echo "The number is 15."
fi

String Comparison Operators:

  • =: Equal to
  • !=: Not equal to
  • -z: Checks if the string is empty
  • -n: Checks if the string is not empty

Example:

#!/bin/bash

string="Hello"

if [ "$string" = "Hello" ]; then
    echo "The string is 'Hello'."
fi

Logical Operators in Bash

Logical operators are used to combine multiple conditions.

Logical Operators:

  • &&: Logical AND
  • ||: Logical OR

Example with &&:

#!/bin/bash

age=25

if [ $age -gt 18 ] && [ $age -lt 30 ]; then
    echo "Age is between 18 and 30."
fi

Example with ||:

#!/bin/bash

color="red"

if [ "$color" = "red" ] || [ "$color" = "blue" ]; then
    echo "The color is either red or blue."
fi

Combining Logical and Comparison Operators:

#!/bin/bash

number=50

if [ $number -lt 40 ] || [ $number -eq 50 ]; then
    echo "The number is less than 40 or exactly 50."
fi

Best Practices for Writing Conditional Statements in Bash

Use Quotation Marks for Strings: Always enclose string variables in double quotes to avoid errors.

if [ "$string" = "value" ]; then

Use Spaces Around Brackets: Ensure proper spacing for conditions.

if [ $variable -eq 10 ]; then

Use Double Brackets for Advanced Conditions: Double brackets ([[ ]]) allow for pattern matching and logical operators without needing escapes.

if [[ $number -gt 5 && $number -lt 15 ]]; then

With these foundational concepts and best practices, you are ready to explore more complex scripts and scenarios in Bash.

Stay tuned for the next topic in this series: Using Loops in Bash!