Conditional Statements (if, else, elif)
Implement decision-making logic in your scripts using 'if', 'else', and 'elif' statements for dynamic behavior.
Conditional Statements (if, else, elif) is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Conditionals Matter
In Bash scripting, sometimes you need your script to make decisions. Imagine a script that performs different actions based on user input or a file's existence.
This is where conditional statements come in! They allow your script to execute specific blocks of code only if certain conditions are met.
The Basic 'if' Statement
The simplest conditional is the if statement. It checks a condition, and if it's true, a block of commands is executed.
The syntax is:
if [ condition ]then# commands if condition is truefi(marks the end of theifblock)
Your First 'if' Script
Let's see a basic if statement in action. This script checks if a number is greater than 5.
Try changing the NUMBER value and running it!
#!/bin/bash
NUMBER=10
if [ $NUMBER -gt 5 ]
then
echo "The number $NUMBER is greater than 5."
fi
echo "Script finished."Crafting Test Conditions
The [ condition ] part is crucial. It's actually a command called test or its alias [.
You can also use [[ condition ]], which is a more advanced version providing better syntax and preventing word splitting issues. For beginners, [ ] is common.
- Always put spaces around the brackets and operators.
- Variables inside
[ ]should usually be quoted (e.g.,"$VARIABLE") to prevent errors with empty or multi-word values.
Comparing Numbers
When comparing numbers in Bash, you use specific operators:
-eq: Equal to-ne: Not equal to-gt: Greater than-ge: Greater than or equal to-lt: Less than-le: Less than or equal to
These are used inside [ ] or [[ ]].
Comparing Strings
For comparing text (strings), different operators are used:
==or=: Equal to (==is preferred in[[ ]])!=: Not equal to-z "$STRING": True if string is empty (zero length)-n "$STRING": True if string is not empty (non-zero length)
Remember to quote your string variables!
The 'if-else' Statement
What if you want to run one block of code if a condition is true, AND a different block if it's false?
That's what if-else is for! It provides an alternative path for your script.
The syntax is:
if [ condition ]then# commands if trueelse# commands if falsefi
'if-else' in Action
This script asks you for a number and tells you if it's positive or not. It uses an if-else structure.
Run it a few times with different inputs (e.g., 5, -3, 0).
#!/bin/bash
read -p "Enter a number: " NUM
if [ "$NUM" -gt 0 ]
then
echo "$NUM is a positive number."
else
echo "$NUM is not a positive number (it's zero or negative)."
fiMultiple Choices with 'elif'
When you have more than two possible outcomes, you can use elif (short for "else if"). This allows you to check multiple conditions in sequence.
The syntax is:
if [ condition1 ]then# commands if condition1 is trueelif [ condition2 ]then# commands if condition1 is false AND condition2 is trueelse# commands if all conditions are falsefi
Advanced Decision Logic
Let's create a script that categorizes a user's age using if, elif, and else. This handles multiple scenarios.
Notice the use of && for combining conditions in [[ ]] (logical AND).
#!/bin/bash
read -p "Enter your age: " AGE
if [[ $AGE -lt 13 ]]
then
echo "You are a child."
elif [[ $AGE -ge 13 && $AGE -lt 18 ]]
then
echo "You are a teenager."
elif [[ $AGE -ge 18 && $AGE -lt 65 ]]
then
echo "You are an adult."
else
echo "You are a senior citizen."
fiConditional Check
Consider the following Bash script:
#!/bin/bash
SCORE=85
if [ $SCORE -lt 60 ]
then
echo "Fail"
elif [ $SCORE -lt 80 ]
then
echo "Pass"
else
echo "Distinction"
fiConditionals Summary
Great job! You've learned how to make your Bash scripts smarter with conditional statements.
- The
ifstatement executes code if a condition is true. elseprovides an alternative path if theifcondition is false.elifallows you to check multiple conditions in a sequence.- Remember to use appropriate operators (
-eqfor numbers,==for strings) and always close yourifblocks withfi.
These tools are fundamental for creating dynamic and interactive scripts!
Frequently asked questions
Is the “Conditional Statements (if, else, elif)” lesson free?
Yes — the full text of “Conditional Statements (if, else, elif)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Conditional Statements (if, else, elif)”?
Implement decision-making logic in your scripts using 'if', 'else', and 'elif' statements for dynamic behavior. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Conditional Statements (if, else, elif)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- First Bash Script & Shebang
- Variables and User Input
- Conditional Statements (if, else, elif)
- Working with Arrays in Bash