Introduction
Imagine you have to sign 1,000 letters. Signing one is easy. Signing 1,000 is boring and hurts your hand. In programming, we hate boring repetition.
To solve this, we use loops. A loop is a way to tell the computer: do this repeatedly, either for every item in a list or until a condition changes.
Loops are central to automation because computers are good at repetition and humans are bad at it. If you catch yourself doing the same command pattern over and over, that is often a sign that a loop belongs there.
In this lab, you will build your own digital assembly lines:
- The Counter: A loop that counts for you (The
forloop). - The Worker: A loop that processes every file in a pile (The
forloop with files). - The Watcher: A loop that keeps working until a goals is met (The
whileloop).
The Number Loop (For Loop)
The for loop is like a checklist. You have a list of items, and you do something for each item on the list.
This is one of the most useful automation patterns in Unix-like systems. Computers are very good at repeating a small action consistently, so loops let you scale a one-item task into a many-item task without copying the same command over and over.
Let's start with the simplest list: numbers. We will ask the computer to count from 1 to 5.
First, check your workspace:
cd ~/project
Create a script named num_loop.sh:
nano num_loop.sh
Type this code:
#!/bin/bash
for i in {1..5}; do
echo "Number: $i"
done
Decoding the Assembly Line:
for i in {1..5}: This is the machine feeder. It takes the numbers 1, 2, 3, 4, 5 and feeds them one by one into the variable$i.do: Start the machine.echo ...: The specific job to do.done: Stop the machine for this item, and go back to get the next one.
The {1..5} part is a Bash shorthand called brace expansion. Before the loop starts, Bash expands it into a list of values. So the loop is really walking through a prepared list, one item at a time.
Save (Ctrl+O) and Exit (Ctrl+X).
Make it run:
chmod +x num_loop.sh
./num_loop.sh
You will see the robot counting:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
The File Loop (For Loop)
Counting is fun, but processing files is useful. The for loop is incredibly powerful when you combine it with wildcards (like *.txt). You can tell the script: "For every text file you find in this folder, do X."
We have already created some sample text files in your folder. Let's write a script to find them.
Create a new file named file_loop.sh:
nano file_loop.sh
Type this code:
#!/bin/bash
echo "Listing text files:"
for file in *.txt; do
echo "Found file: $file"
done
The Logic:
Instead of numbers {1..5}, we give the loop a pattern *.txt.
BASH looks at the folder, finds all matching files (like data1.txt, notes.txt), and feeds them one by one into the variable called $file.
That matching step happens before the loop body runs. The shell expands *.txt into a list of filenames, and then the for loop iterates through that list. This is why loops and glob patterns work so well together in shell scripting.
Save and Exit.
Run the script:
chmod +x file_loop.sh
./file_loop.sh
Output:
Listing text files:
Found file: data1.txt
Found file: data2.txt
Found file: notes.txt
Imagine if instead of just echo, you used mv to move them or zip to archive them. You could process 10,000 files in seconds with this same 4-line script!
The Condition Loop (While Loop)
The for loop is great when you have a list (of numbers or files). But what if you don't have a list? What if you just want to "Keep working UNTIL something happens"?
This is the job of the While Loop. It's like a thermostat: "While the room is cold, keep the heater on."
Let's write a script that counts to 10 manually.
Create while_loop.sh:
nano while_loop.sh
Type this code:
#!/bin/bash
counter=1
while [ $counter -le 10 ]; do
echo "Count: $counter"
((counter++))
done
Decoding the Thermostat:
while [ $counter -le 10 ]: The Condition. "Is the counter Less than or Equal to 10?"- If YES: Enter the loop.
- If NO: Stop.
((counter++)): The Update. This adds 1 to the counter. Critical: If you forget this line, the counter stays at 1 forever, and the script runs forever (an "Infinite Loop")!.
This highlights the key difference between for and while. A for loop usually receives its sequence from the shell up front. A while loop depends on a condition that your script must update correctly while it runs.
The ((counter++)) syntax is Bash arithmetic notation. It tells the shell to treat counter as a number and increase it by one. Shell scripts often mix text processing and simple arithmetic, so this syntax appears frequently.
Save and Exit.
Run it:
chmod +x while_loop.sh
./while_loop.sh
It behaves like the for loop, but the logic is different. For runs through a list. While runs on a condition.
Summary
You have built your first automation engines.
In this lab, you mastered:
- The Checklist (
for): Processing a defined list of items (like numbers{1..5}). - The Batch Processor (
for *.txt): Automatically handling every file in a directory. - The Thermostat (
while): Repeating actions until a specific goal or limit is reached.
These loops are the workhorses of scripting. They turn 1,000 manual clicks into 1 second of computer time.



