While and Until Loops in Shell
Summary: Execute code repeatedly using while and until.
When automating tasks or scripting in the shell, loops allow you to repeat actions efficiently. Among the most commonly used loops are the while and until constructs. Understanding their differences, syntax, and practical uses is essential for any shell scripter. In this article, we’ll explore how while and until loops work in shell scripts with real examples.
The while Loop
A while loop executes its body as long as a specified condition is true. It’s ideal when you want your script to repeat a block of code for an undetermined number of times, stopping once a condition is no longer satisfied.
Syntax
while [ condition ]; do
# Commands to execute
done
Example: Countdown Timer
Let’s create a simple countdown timer that starts from 5 and stops at 0.
count=5
while [ $count -gt 0 ]; do
echo "Countdown: $count"
sleep 1
count=$((count - 1))
done
echo "Blast off!"
How it works:
- The loop checks if
countis greater than 0. - It prints the current count, waits for 1 second, and decrements the count.
- Once
countreaches 0, the condition fails, and the loop exits.
Common Use Cases
- Monitoring files or system status until a condition changes
- Reading lines from a file
- Waiting for user input or a process to complete
The until Loop
The until loop is nearly the opposite of the while loop. Its body executes repeatedly as long as the condition is false. It only stops when the condition becomes true.
Syntax
until [ condition ]; do
# Commands to execute
done
Example: Waiting for a File to Exist
Suppose you want your script to wait until a specific file appears in a directory:
until [ -f /tmp/target-file.txt ]; do
echo "Waiting for target-file.txt to be created..."
sleep 2
done
echo "File found!"
How it works:
- The
-ftest checks if the file exists. - As long as the file does not exist (
[ -f ... ]is false), the script prints a message and waits. - When the file is created, the condition becomes true and the loop ends.
Common Use Cases
- Waiting for a resource to appear or become available
- Retrying commands until successful
- Polling for changes in the system or environment
Comparison: When to Use Each Loop
| Use This | When You Want To... |
|---|---|
while |
Keep looping WHILE a condition is true |
until |
Keep looping UNTIL a condition is true |
In essence, the two loops can often achieve the same outcomes with opposite conditions, but choosing the one that directly maps to your intent improves readability.
Advanced: Reading Files with while
A practical and popular usage is reading files line by line:
while IFS= read -r line; do
echo "Line: $line"
done < myfile.txt
- The loop reads each line from
myfile.txtand processes it. - Using
IFS=and-rprevents accidental trimming and backslash interpretation.
Good Practices
- Avoid Infinite Loops: Ensure your condition can eventually become false (
while) or true (until), or use abreakstatement inside the loop. - Quoting Variables: Always quote variables in test expressions to avoid issues with whitespace or empty values.
while [ "$input" != "q" ]; do # do something done
Conclusion
while and until loops are foundational tools for automation in shell scripts. Remember:
- Use
whilefor repeating actions while a condition is true. - Use
untilfor repeating actions until a condition becomes true.
Choosing the right loop for the job makes your script clearer and more robust. Happy scripting!
Further Reading: