Chapter 4: Conditional Statements
Master conditional statements like if
, elif
, and else
to control the flow of your script and make it responsive to different conditions and inputs.
In this chapter, we'll explore conditional statements in Bash, which allow scripts to make decisions based on specific conditions. This flexibility is essential for creating responsive and adaptive scripts.
Basic Structure of Conditional Statements
Bash conditional statements use the if
keyword to check if a condition is true and, if so, execute a block of code. The else
and elif
keywords provide alternative branches.
# Basic syntax of if-else
if [ condition ]; then
# Code to execute if condition is true
else
# Code to execute if condition is false
fi
Using the if
Statement
The if
statement is the core of conditional logic in Bash. It checks whether a condition is true and executes code accordingly.
# Example: Simple if statement
number=10
if [ $number -gt 5 ]; then
echo "The number is greater than 5"
fi
In this example, the script checks if number
is greater than 5. If true, it outputs a message. If false, it does nothing.
Adding the else
Statement
The else
statement provides an alternative action if the condition is not met:
# Example: if-else statement
number=3
if [ $number -gt 5 ]; then
echo "The number is greater than 5"
else
echo "The number is 5 or less"
fi
Here, the script outputs "The number is 5 or less"
if number
is not greater than 5.
Using elif
for Multiple Conditions
The elif
(else if) statement allows for additional conditions, letting the script execute different code blocks based on several possibilities.
# Example: if-elif-else statement
number=7
if [ $number -gt 10 ]; then
echo "The number is greater than 10"
elif [ $number -eq 7 ]; then
echo "The number is exactly 7"
else
echo "The number is less than 10 and not 7"
fi
In this example, if number
equals 7, the script outputs "The number is exactly 7"
; otherwise, it checks other conditions.
Comparison Operators in Bash
Bash provides various comparison operators to use within conditional statements:
-eq
: Equal to (e.g.,[ $a -eq $b ]
)-ne
: Not equal to-gt
: Greater than-lt
: Less than-ge
: Greater than or equal to-le
: Less than or equal to
These operators enable numeric comparisons, while =
and !=
are used for string comparisons.
Logical Operators
Logical operators allow you to combine multiple conditions within a single statement:
-a
: Logical AND (e.g.,[ $a -gt 5 -a $b -lt 10 ]
)-o
: Logical OR!
: Logical NOT
# Example: using AND operator
age=20
if [ $age -gt 18 -a $age -lt 65 ]; then
echo "Working age"
fi
Nested Conditionals
You can nest conditional statements to create more complex logic in your scripts:
# Example: Nested if statements
age=30
if [ $age -gt 18 ]; then
if [ $age -lt 65 ]; then
echo "Adult in working age"
else
echo "Senior"
fi
else
echo "Minor"
fi
Summary and Next Steps
In this chapter, we explored conditional statements, including if
, elif
, and else
, along with comparison and logical operators. In the next chapter, we'll delve into loops, which allow scripts to perform repetitive tasks efficiently.