184 lines
2.9 KiB
Markdown
Raw Normal View History

# Loops in Bash
Loops are fundamental constructs in programming that allow you to execute a set of commands repeatedly. In Bash scripting, loops are essential for automating repetitive tasks, processing multiple files, and working with data structures. This tutorial will cover the three main types of loops in Bash: for loops, while loops, and until loops.
## 1. For Loops
The 'for' loop is used to iterate over a list of items or a range of values.
Syntax:
2024-09-07 10:14:32 -06:00
```
for variable in list
do
2024-09-07 10:14:32 -06:00
commands
done
```
Examples:
- Iterating over a list of items:
2024-09-07 10:14:32 -06:00
```
for fruit in apple banana orange
do
2024-09-07 10:14:32 -06:00
echo "I like $fruit"
done
```
- Iterating over a range of numbers:
2024-09-07 10:14:32 -06:00
```
for i in {1..5} #{1..5} is using brace expansion for everything in the range of 1-5
do
2024-09-07 10:14:32 -06:00
echo "Number: $i"
done
```
- C-style for loop:
2024-09-07 10:14:32 -06:00
```
for ((i=0; i<5; i++))
do
2024-09-07 10:14:32 -06:00
echo "Count: $i"
done
```
- Iterating over files in a directory:
2024-09-07 10:14:32 -06:00
```
for file in *.txt
do
echo "Processing $file"
# Add commands to process each file
done
```
## 2. While Loops
The 'while' loop executes a set of commands as long as a given condition is true.
Syntax:
2024-09-07 10:14:32 -06:00
```
while condition
do
2024-09-07 10:14:32 -06:00
commands
done
```
Examples:
- Basic while loop:
2024-09-07 10:14:32 -06:00
```
count=1
2024-09-07 10:14:32 -06:00
while [ $count -le 5 ] # -le is less than or equal to
do
2024-09-07 10:14:32 -06:00
echo "Count: $count"
((count++))
done
```
- Reading input until a condition is met:
2024-09-07 10:14:32 -06:00
```
while read -p "Enter a number (0 to exit): " num # the read command takes user input and stores it as a variable
do
2024-09-07 10:14:32 -06:00
if [ "$num" -eq 0 ]; then
echo "Exiting..."
break
fi
echo "You entered: $num"
done
```
## 3. Until Loops
The 'until' loop is similar to the while loop, but it executes commands until a condition becomes true.
Syntax:
2024-09-07 10:14:32 -06:00
```
until condition
do
2024-09-07 10:14:32 -06:00
commands
done
```
Example:
2024-09-07 10:14:32 -06:00
```
count=1
until [ $count -gt 5 ]
do
2024-09-07 10:14:32 -06:00
echo "Count: $count"
((count++))
done
```
## 4. Loop Control Statements
Bash provides two main loop control statements:
- break: Exits the loop immediately
- continue: Skips the rest of the current iteration and moves to the next one
Example using both:
2024-09-07 10:14:32 -06:00
```
for i in {1..10}
do
2024-09-07 10:14:32 -06:00
if [ $i -eq 5 ]; then
continue
fi
if [ $i -eq 8 ]; then
break
fi
echo "Number: $i"
done
```
## 5. Advanced Loop Techniques
- Nested loops:
2024-09-07 10:14:32 -06:00
```
for i in {1..3}
do
2024-09-07 10:14:32 -06:00
for j in {1..3}
do
echo "i=$i, j=$j"
done
done
```
- Looping through arrays:
2024-09-07 10:14:32 -06:00
```
declare -a fruits=("apple" "banana" "orange" "grape")
for fruit in "${fruits[@]}"
do
2024-09-07 10:14:32 -06:00
echo "I like $fruit"
done
```
- Infinite loops (use with caution):
2024-09-07 10:14:32 -06:00
```
while true # the "true" command always returns TRUE -so this will loop forever!
do
2024-09-07 10:14:32 -06:00
echo "This will run forever unless interrupted"
sleep 1
done
```
- Looping with command substitution:
2024-09-07 10:14:32 -06:00
```
for line in $(cat file.txt) # $() is command substitution - you can put a command in here and the output will be used!
do
2024-09-07 10:14:32 -06:00
echo "Line: $line"
done
```
- Parallel execution of loops:
2024-09-07 10:14:32 -06:00
```
for job in job1 job2 job3
do
2024-09-07 10:14:32 -06:00
(
echo "Starting $job"
sleep 2
echo "Finished $job"
) &
done
wait
echo "All jobs completed"
```