Learn how to create infinite loops in bash scripts on Linux. This tutorial provides examples and syntax for setting up looping forever in a bash script 🤓
Crafting Infinite Loops in Bash Scripting
Loops are a crucial construct in Bash scripting that enable repeating a block of code until a condition is met. Infinite loops, as the name suggests, iterate indefinitely and require manual intervention to stop. Though risky, infinite loops serve various use cases in Bash. This guide covers creating and controlling infinite loops in Bash scripts.
What Are Infinite Loops?
An infinite loop executes a sequence of instructions endlessly, either due to:
- The loop having no terminating condition.
- A condition that is impossible to meet.
- A condition that causes the loop to repeat.
Infinite loops are also called endless or forever loops.
How to Construct an Infinite Loop in Bash
The three loop constructs in Bash – for, while, and until – can all create infinite loops with minor modifications.
Using the while Loop
The while loop syntax in Bash is:
while condition; do commands done
To make it an infinite loop, the condition can be true, the null command :, or a condition that is always true:
while :; do echo "Infinite loop. To stop, press CTRL+C." sleep 1 done
This prints a message indefinitely every 1 second, until forcibly stopped with CTRL+C.
Using the for Loop
The for loop syntax is:
for variable in list; do commands done
Omitting all three expressions creates an infinite loop:
for (( ; ; )); do echo "Infinite for loop. To stop, press CTRL+C." sleep 2 done
Using the until Loop
The until loop syntax is:
until condition; do commands done
Using a condition that is always false like false creates an infinite loop:
until false; do echo "Infinite until loop. To stop, press CTRL+C." sleep 5 done
Controlling Infinite Loops in Bash
Though useful, infinite loops can freeze your terminal session. Here are techniques to control infinite loops:
1. Checking for User Input
Check if the user wants to stop the loop:
while true; do read -p "Type 'exit' to stop: " input [[ $input == "exit" ]] && break # ...loop commands... done
2. Using Loop Control Commands
The break and continue commands alter loop execution.
To exit a loop completely, use break:
while :; do #...loop commands... [[ condition ]] && break done
To skip the current iteration, use continue:
while :; do #...loop commands... [[ condition ]] && continue #...more commands... done
3. Tracking Time or Iterations
Exit after a time limit or number of iterations:
iterations=0 while :; do #...loop commands... ((iterations++)) [[ $iterations -eq 10 ]] && break done
4. Encapsulating The Loop
Run the loop in a subshell to avoid freezing the terminal:
( while :; do #...infinite loop... done )
When To Use Infinite Loops in Bash
Some examples of using infinite loops:
- Creating a daemon or background process
- Reading and processing continuous user input
- Writing log messages indefinitely
- Repeating a task at an interval
- Benchmarking code performance
However, infinite loops can make scripts difficult to manage. It’s best to include a termination condition where possible.
In linux shell scripting, infinite loops provide a way to execute a set of commands continuously until explicitly terminated from an external source. You can create infinite loops using while, for, or until loops by either omitting exit conditions or defining conditions that remain perpetually true. Managing these loops involves careful monitoring, such as tracking variables, incorporating user input or time-based checks to control the loop’s behavior. It’s essential to be cautious when using infinite loops to prevent system resource consumption or terminal freezes, often achieved by setting limits on iterations or implementing conditional checks for safe and controlled execution. Bash’s infinite loops require careful handling to prevent system resource consumption and issues.