Conditional Statements – Bash
Welcome to this comprehensive, student-friendly guide on conditional statements in Bash! 🎉 Whether you’re just starting out or looking to solidify your understanding, this tutorial will walk you through everything you need to know. We’ll explore the basics, dive into examples, and tackle common questions and issues. Let’s get started! 🚀
What You’ll Learn 📚
- Understanding the purpose of conditional statements in Bash
- Key terminology and concepts
- Simple and complex examples
- Common questions and troubleshooting tips
Introduction to Conditional Statements
Conditional statements are like the decision-makers in your Bash scripts. They allow your script to take different actions based on certain conditions. Think of them as the traffic lights of programming: they help control the flow of your script based on what’s happening at any given moment.
Key Terminology
- Condition: A statement that evaluates to true or false.
- If statement: Executes a block of code if a condition is true.
- Else statement: Executes a block of code if the condition is false.
- Elif statement: Short for ‘else if’, allows checking multiple conditions.
Simple Example: The Basics
#!/bin/bash
# Simple if statement example
if [ 1 -eq 1 ]; then
echo "This is true!"
fi
This script checks if 1 is equal to 1. Since this is true, it prints “This is true!”.
Expected Output:
This is true!
Progressively Complex Examples
Example 1: If-Else Statement
#!/bin/bash
# If-else statement example
number=10
if [ $number -gt 5 ]; then
echo "The number is greater than 5."
else
echo "The number is 5 or less."
fi
Here, we check if the variable number
is greater than 5. If true, it prints the first message; otherwise, it prints the second.
Expected Output:
The number is greater than 5.
Example 2: If-Elif-Else Statement
#!/bin/bash
# If-elif-else statement example
number=3
if [ $number -gt 5 ]; then
echo "The number is greater than 5."
elif [ $number -eq 5 ]; then
echo "The number is exactly 5."
else
echo "The number is less than 5."
fi
This example introduces elif
to check multiple conditions. It prints a message based on the value of number
.
Expected Output:
The number is less than 5.
Example 3: Nested If Statements
#!/bin/bash
# Nested if statement example
number=7
if [ $number -gt 5 ]; then
if [ $number -lt 10 ]; then
echo "The number is between 6 and 9."
else
echo "The number is 10 or more."
fi
else
echo "The number is 5 or less."
fi
Nesting allows for more complex decision-making by placing an if
statement inside another if
.
Expected Output:
The number is between 6 and 9.
Common Questions and Answers
- What is the purpose of conditional statements?
They control the flow of your script by executing different code blocks based on conditions.
- How do I compare strings in Bash?
Use
==
for equality and!=
for inequality, e.g.,[ "$str1" == "$str2" ]
. - Can I use logical operators in conditions?
Yes, use
-a
for AND and-o
for OR, e.g.,[ $a -gt 5 -a $b -lt 10 ]
. - Why isn’t my if statement working?
Check your syntax, ensure spaces around brackets, and verify your condition logic.
- How do I handle multiple conditions?
Use
elif
for multiple conditions or logical operators for combined conditions.
Troubleshooting Common Issues
Ensure you have spaces around brackets and operators. Bash syntax is sensitive to spaces!
Use
set -x
at the start of your script to debug and see how your script executes each line.
Practice Exercises
- Write a script that checks if a number is positive, negative, or zero.
- Create a script that prints “Weekend” if the current day is Saturday or Sunday, otherwise “Weekday”.
For more details, check the Bash Manual on Conditional Constructs.