Loops in Bash
Loops are essential for automating repetitive tasks, like processing a batch of files.
For Loops
You can loop over a list of items or a sequence of numbers.
#!/bin/bash
# Looping through a specific list
for color in red blue green; do
echo "Color: $color"
done
# Looping through a range
for i in {1..5}; do
echo "Processing batch $i..."
done
While Loops
While loops run as long as a condition is true. They are often used to read files line by line.
count=1
while [ $count -le 3 ]; do
echo "Count is $count"
((count++))
done
booting...