Introduction
In the world of the command line, repetition is a sign that you have an opportunity to automate. Whether you’re renaming a hundred files, processing lines in a log, or running a command on multiple servers, doing it manually is slow and prone to error. This is where Bash loops come in.
Loops are a fundamental concept in scripting that allow you to execute a block of commands multiple times. The two most common loops you’ll use in Bash are the for loop and the while loop. This guide will walk you through how to use both to automate your repetitive tasks and become a more efficient Linux user.
The for Loop: Iterating Over a List
A for loop is perfect when you have a known, finite list of items you want to operate on. This “list” can be a set of strings, a range of numbers, or a collection of files.
Basic Syntax:
for variable in item1 item2 item3
do
# Commands to execute for each item
echo "Current item: $variable"
done
Example 1: Looping Through a List of Strings
Let’s say you want to check the status of several web servers. You can use a for loop to iterate through a list of their hostnames.
Instructions:
- Create a new script file named
check_servers.sh. - Add the following code. The
ping -c 1command sends a single packet to check if the host is reachable.
#!/bin/bash
# A list of servers to check
SERVERS="google.com anothersite.com 8.8.8.8"
echo "--- Checking server status ---"
for server in $SERVERS
do
echo "Pinging $server..."
ping -c 1 "$server"
echo "---"
done
echo "--- Server check complete ---"
- Make the script executable and run it.
chmod +x check_servers.sh
./check_servers.sh
Example 2: Looping Through a Range of Numbers
Brace expansion ({start..end}) is a powerful feature that lets you generate sequences. This is incredibly useful for creating or managing numbered files and directories.
Instructions: Let’s create five numbered backup directories.
- Open your terminal.
- Run the following one-liner. The loop will iterate through numbers 1 to 5 and create a directory for each.
for i in {1..5}
do
echo "Creating backup_dir_$i"
mkdir "backup_dir_$i"
done
After running, you can list the contents of your directory with ls to see the newly created folders.
Example 3: Looping Through Files (Batch Renaming)
This is one of the most practical uses for a for loop. Let’s say you have a folder full of .jpeg files that you want to rename to .jpg.
Instructions:
- Navigate to a directory containing some image files (or create some for testing:
touch photo_A.jpeg photo_B.jpeg). - Run this loop directly in your terminal.
for file in *.jpeg
do
# Use parameter expansion to get the filename without the extension
new_name="${file%.jpeg}.jpg"
echo "Renaming '$file' to '$new_name'"
mv -- "$file" "$new_name"
done
*.jpeg: This wildcard matches all files ending with.jpeg.${file%.jpeg}: This is a shell parameter expansion. It takes the value of thefilevariable and removes the.jpegsuffix from the end.mv --: Renames the file. The--is a good practice that prevents issues if a filename starts with a dash.
The while Loop: Repeating While a Condition is True
A while loop is used when you don’t know how many times you need to loop. It continues to execute a block of commands as long as a specific condition remains true.
Basic Syntax:
while [ condition ]
do
# Commands to execute
done
Warning: You must ensure that something inside the loop eventually makes the condition false, otherwise you will create an infinite loop!
Example 1: A Simple Counter
This is a classic example to understand the mechanics of a while loop.
Instructions:
- Create a script named
counter.sh. - Add the following code.
#!/bin/bash
counter=1
while [ $counter -le 5 ]
do
echo "Count: $counter"
# Increment the counter
counter=$((counter + 1))
done
echo "Loop finished!"
[ $counter -le 5 ]: This is the test condition.-lemeans “less than or equal to”. The loop continues as long as the counter is 5 or less.counter=$((counter + 1)): This is the crucial part that increments the counter. Without it,$counterwould always be 1, and the loop would run forever.
Example 2: Reading a File Line by Line
A powerful and common use for while loops is to process a file one line at a time. This is perfect for parsing log files or reading a list of inputs.
Instructions:
- First, create a file named
hosts.txtwith a few domain names, each on a new line.
# hosts.txt
google.com
github.com
cloudflare.com
- Now, create a script named
process_hosts.shto read this file.
#!/bin/bash
FILENAME="hosts.txt"
while IFS= read -r line
do
echo "Processing host: $line"
# You could run any command here, e.g., ssh, ping, curl
done < "$FILENAME"
while IFS= read -r line: This is the standard, safe way to read a file line by line in Bash.read -r: The-roption preventsreadfrom interpreting backslashes as escape characters.IFS=: Setting the Internal Field Separator to empty preventsreadfrom trimming leading/trailing whitespace from lines.
done < "$FILENAME": This is input redirection. It feeds the content of$FILENAMEinto thewhileloop.
Conclusion
You’ve now seen the power of for and while loops in Bash. They are the cornerstone of shell scripting and automation.
- Use a
forloop when you have a definite set of items to iterate over (e.g., files in a directory, a list of servers, a range of numbers). - Use a
whileloop when you need to repeat a task until a condition changes (e.g., reading a file until the end, waiting for a process to finish).
The next step is to start looking for repetitive tasks in your own workflow. Start small. Do you find yourself renaming a series of files often? Or running the same command on multiple servers? Try writing a small loop to automate it. With a little practice, you’ll be saving yourself hours of tedious work.