Conditional statements are fundamental in any programming language, including Bash. They allow you to make decisions in your scripts based on certain conditions. In this section, we will cover the basics of conditional statements in Bash, including if, else, elif, and case statements.
Key Concepts
- if Statement: Executes a block of code if a specified condition is true.
- else Statement: Executes a block of code if the condition in the
ifstatement is false. - elif Statement: Stands for "else if" and allows you to check multiple conditions.
- case Statement: A multi-way branch statement that is useful for handling multiple conditions.
if Statement
The if statement is used to test a condition. If the condition is true, the block of code inside the if statement is executed.
Syntax
Example
Explanation
number=10: Assigns the value 10 to the variablenumber.[ $number -gt 5 ]: Checks ifnumberis greater than 5.echo "The number is greater than 5": Prints the message if the condition is true.
else Statement
The else statement is used to execute a block of code if the condition in the if statement is false.
Syntax
if [ condition ]; then
# code to execute if condition is true
else
# code to execute if condition is false
fiExample
#!/bin/bash
number=3
if [ $number -gt 5 ]; then
echo "The number is greater than 5"
else
echo "The number is not greater than 5"
fiExplanation
number=3: Assigns the value 3 to the variablenumber.[ $number -gt 5 ]: Checks ifnumberis greater than 5.echo "The number is not greater than 5": Prints the message if the condition is false.
elif Statement
The elif statement allows you to check multiple conditions.
Syntax
if [ condition1 ]; then
# code to execute if condition1 is true
elif [ condition2 ]; then
# code to execute if condition2 is true
else
# code to execute if none of the conditions are true
fiExample
#!/bin/bash
number=7
if [ $number -gt 10 ]; then
echo "The number is greater than 10"
elif [ $number -gt 5 ]; then
echo "The number is greater than 5 but less than or equal to 10"
else
echo "The number is 5 or less"
fiExplanation
number=7: Assigns the value 7 to the variablenumber.[ $number -gt 10 ]: Checks ifnumberis greater than 10.[ $number -gt 5 ]: Checks ifnumberis greater than 5 but less than or equal to 10.echo "The number is greater than 5 but less than or equal to 10": Prints the message if the second condition is true.
case Statement
The case statement is used to simplify complex conditional statements. It is similar to the switch statement in other programming languages.
Syntax
case expression in
pattern1)
# code to execute if expression matches pattern1
;;
pattern2)
# code to execute if expression matches pattern2
;;
*)
# code to execute if expression does not match any pattern
;;
esacExample
#!/bin/bash
day="Monday"
case $day in
"Monday")
echo "Start of the work week"
;;
"Friday")
echo "End of the work week"
;;
"Saturday" | "Sunday")
echo "Weekend!"
;;
*)
echo "Midweek day"
;;
esacExplanation
day="Monday": Assigns the value "Monday" to the variableday.case $day in: Starts thecasestatement, checking the value ofday."Monday"): Matches the value "Monday".echo "Start of the work week": Prints the message if the value is "Monday"."Saturday" | "Sunday"): Matches either "Saturday" or "Sunday".echo "Weekend!": Prints the message if the value is "Saturday" or "Sunday".
Practical Exercises
Exercise 1: Simple if Statement
Write a script that checks if a number is positive.
Solution
#!/bin/bash
number=-5
if [ $number -gt 0 ]; then
echo "The number is positive"
else
echo "The number is not positive"
fiExercise 2: Using elif
Write a script that categorizes a number as positive, negative, or zero.
#!/bin/bash
number=0
if [ $number -gt 0 ]; then
echo "The number is positive"
elif [ $number -lt 0 ]; then
echo "The number is negative"
else
echo "The number is zero"
fiSolution
#!/bin/bash
number=0
if [ $number -gt 0 ]; then
echo "The number is positive"
elif [ $number -lt 0 ]; then
echo "The number is negative"
else
echo "The number is zero"
fiExercise 3: case Statement
Write a script that prints a message based on the day of the week.
#!/bin/bash
day="Wednesday"
case $day in
"Monday")
echo "Start of the work week"
;;
"Friday")
echo "End of the work week"
;;
"Saturday" | "Sunday")
echo "Weekend!"
;;
*)
echo "Midweek day"
;;
esacSolution
#!/bin/bash
day="Wednesday"
case $day in
"Monday")
echo "Start of the work week"
;;
"Friday")
echo "End of the work week"
;;
"Saturday" | "Sunday")
echo "Weekend!"
;;
*)
echo "Midweek day"
;;
esacCommon Mistakes and Tips
- Using
=instead of-eqfor numeric comparison: In Bash,=is used for string comparison, while-eqis used for numeric comparison. - Forgetting to close
ifstatements withfi: Always ensure that yourifstatements are properly closed withfi. - Incorrect syntax in
casestatements: Ensure that each pattern ends with)and each block ends with;;.
Conclusion
In this section, we covered the basics of conditional statements in Bash, including if, else, elif, and case statements. These constructs are essential for making decisions in your scripts and handling different conditions effectively. Practice the exercises provided to reinforce your understanding and prepare for more advanced scripting techniques.
Bash Programming Course
Module 1: Introduction to Bash
Module 2: Basic Bash Commands
- File and Directory Operations
- Text Processing Commands
- File Permissions and Ownership
- Redirection and Piping
