Looping Constructs (for, while)
Automate repetitive tasks using 'for' and 'while' loops, iterating over lists, files, or conditions.
Looping Constructs (for, while) is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 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.
Automating Tasks with Loops
Welcome! In this lesson, we'll dive into the world of loops in Bash scripting. Loops are powerful tools that allow your scripts to repeat a set of commands multiple times.
This is essential for automating repetitive tasks, processing lists of data, or performing actions until a certain condition is met. We'll explore two main types: for loops and while loops.
The 'for' Loop: Item Lists
The for loop is perfect for iterating over a fixed list of items. This list can be a series of words, filenames, numbers, or anything you can define.
Its basic syntax is:
forvariableinitem1 item2 ...; do- commands to repeat
done
Each item in the list will be assigned to variable, and the commands will run for each one.
Example: Iterating a List
Let's see the for loop in action by iterating through a list of fruits. Try running this example to see how each fruit is processed:
#!/bin/bash
echo "My favorite fruits:"
for fruit in Apple Banana Orange;
do
echo "$fruit is delicious!"
done'for' Loop: Number Sequences
The for loop can also easily generate and iterate through number sequences. This is very handy for tasks that require counting or processing items by index.
You can use brace expansion {start..end} to create a sequence of numbers:
for i in {1..5}; do ... done
Alternatively, the seq command can generate sequences with more options, like steps:
for i in $(seq 1 2 10); do ... done
Example: Counting with 'for'
Here's how you can use brace expansion to count from 1 to 3. This is a common pattern for simple numerical loops.
#!/bin/bash
echo "Counting up:"
for i in {1..3};
do
echo "Number: $i"
doneThe 'while' Loop: Conditional Repetition
The while loop executes a block of commands repeatedly as long as a specified condition remains true. It's ideal when you don't know in advance how many times you need to loop.
Its basic syntax is:
while[ condition ]; do- commands to repeat
done
The condition is evaluated before each iteration. If it's true, the commands run; if false, the loop ends.
Example: Simple 'while' Counter
This example demonstrates a while loop that counts from 1 to 3. Notice how the count variable is incremented inside the loop to eventually make the condition false.
#!/bin/bash
count=1
echo "Counting with while loop:"
while [ $count -le 3 ];
do
echo "Current count: $count"
count=$((count + 1))
done'while' Loop: Reading Files Line by Line
A very common and powerful use of the while loop is to read and process a file line by line. This is crucial for parsing log files, configuration files, or any text data.
The pattern often looks like this:
while IFS= read -r line; do- commands to process $line
done < filename.txt
IFS= read -r is a robust way to read lines, preventing issues with whitespace and backslashes.
Example: Processing a File
Let's create a temporary file and use a while loop to read and display its content line by line. This shows a practical application of file processing.
#!/bin/bash
# Create a temporary file for demonstration
echo "First line of data" > temp_data.txt
echo "Second line of data" >> temp_data.txt
echo "Third line of data" >> temp_data.txt
echo "\nReading from temp_data.txt:"
while IFS= read -r line;
do
echo "Processed: $line"
done < temp_data.txt
# Clean up the temporary file
rm temp_data.txtLoop Type Selection
You've learned about both for and while loops. Each has its strengths depending on the task at hand.
Consider the following scenario: You need to process each filename in a specific directory.
Recap: Loops for Automation
You've successfully explored Bash looping constructs!
- The
forloop is ideal for iterating over a known, finite list of items or a sequence of numbers. - The
whileloop is best when you need to repeat commands as long as a certain condition remains true, often used for counting or reading files line by line.
Mastering these loops is fundamental for writing powerful and automated Bash scripts. Keep practicing to build your scripting prowess!
Frequently asked questions
Is the “Looping Constructs (for, while)” lesson free?
Yes — the full text of “Looping Constructs (for, while)” 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 “Looping Constructs (for, while)”?
Automate repetitive tasks using 'for' and 'while' loops, iterating over lists, files, or conditions. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Looping Constructs (for, while)” 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
- Looping Constructs (for, while)
- Functions in Bash Scripts
- Script Arguments and Options ($@, $#, getopts)
- The case Statement & Pattern Matching