for var in item1 item2 item3; do
echo "$var"
donefor i in {1..5}; do
echo "Number $i"
doneOutput:
Number 1
Number 2
Number 3
Number 4
Number 5
for i in {0..20..5}; do
echo "$i"
doneOutput: 0 5 10 15 20
for ((i=0; i<5; i++)); do
echo "i = $i"
donefor file in *.txt; do
echo "Processing $file"
donefor user in $(cut -d: -f1 /etc/passwd); do
echo "User: $user"
donefor i in {1..3}; do
for j in {a..c}; do
echo "$i$j"
done
donefor (( ; ; )); do
echo "Infinite loop, press Ctrl+C to exit"
sleep 1
donefor i in {1..5}; do echo "Hello $i"; donecount=1
while [[ $count -le 5 ]]; do
echo "Count = $count"
((count++))
donewhile read line; do
echo "Line: $line"
done < file.txtcount=1
until [[ $count -gt 5 ]]; do
echo "Count = $count"
((count++))
donewhile true; do
echo "Running..."
sleep 1
donewhile read user; do
echo "User: $user"
done < <(cut -d: -f1 /etc/passwd)while read -p "Enter something (or 'quit'): " input; do
[[ $input == "quit" ]] && break
echo "You typed: $input"
donecount=1; while [[ $count -le 3 ]]; do echo "Loop $count"; ((count++)); done| Statement | Meaning |
|---|---|
break |
Exit the loop immediately |
continue |
Skip current iteration |
exit |
Exit the script completely |
Examples:
for i in {1..5}; do
if [[ $i -eq 3 ]]; then
echo "Breaking at $i"
break
fi
echo "$i"
donefor i in {1..5}; do
if [[ $i -eq 3 ]]; then
echo "Skipping $i"
continue
fi
echo "$i"
done- Loop over arguments
for arg in "$@"; do
echo "Arg: $arg"
done- Parallel commands
for site in google.com github.com; do
ping -c1 $site & # run in background
done
wait- Arithmetic loop with while
i=0
while (( i < 5 )); do
echo $i
((i++))
done- Colorized output in loops
for i in {1..5}; do
echo -e "\e[32mIteration $i\e[0m"
done- Process substitution
while read line; do
echo ">> $line"
done < <(ls -1)- Loop with timeout
end=$((SECONDS+5))
while [ $SECONDS -lt $end ]; do
echo "Looping..."
sleep 1
done- Nested while with file + counter
count=1
while read line; do
echo "$count: $line"
((count++))
done < /etc/passwd- Simple loop
for i in {1..5}; do echo "hello $i"; done- Infinite loop in terminal
while true; do date; sleep 1; done- Loop until user presses Ctrl+C
for (( ; ; )); do echo "Running"; sleep 2; doneβ
Summary:
In Bash, loops (for, while, until) let you:
- Iterate over numbers, files, strings, arguments
- Read input from files or commands
- Run infinitely (until stopped)
- Control flow with
break,continue,exit - Use both in scripts and one-liners in terminal
until [ condition ]
do
commands
done- Runs until the condition becomes true
- Opposite of
whileloop (which runs while condition is true)
count=1
until [ $count -gt 5 ]
do
echo "Count = $count"
((count++))
doneπ Output:
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
until false
do
echo "This runs forever (press Ctrl+C to stop)"
donen=5
until [ $n -eq 0 ]
do
echo "Countdown: $n"
((n--))
done
echo "Blastoff!"file="/tmp/myfile.txt"
until [ -f "$file" ]
do
echo "Waiting for $file ..."
sleep 2
done
echo "$file is ready!"x=0
until [ $x -ge 10 ]
do
((x++))
if (( x == 5 )); then
echo "Skipping 5"
continue
fi
if (( x == 8 )); then
echo "Stopping at 8"
break
fi
echo "x = $x"
doneuntil ping -c1 google.com &>/dev/null; do echo "No internet... retrying"; sleep 2; done
echo "Connected!"| Feature | while loop |
until loop |
|---|---|---|
| Condition | Runs while condition is true | Runs until condition is true |
| Usage style | "Do this as long as..." | "Do this until..." |
| Infinite by default | while true; do ...; done |
until false; do ...; done |
β
Summary:
Use while when you know what condition must stay true.
Use until when you want to wait for a condition to become true (e.g., file exists, process ends, internet available).
- Ends the current loop entirely.
- Control moves to the next statement after the loop.
for i in {1..10}
do
if [ $i -eq 5 ]; then
echo "Breaking at $i"
break
fi
echo "i = $i"
doneπ Output:
i = 1
i = 2
i = 3
i = 4
Breaking at 5
- Skips the rest of the loop body for the current iteration.
- Moves to the next loop cycle.
for i in {1..7}
do
if [ $i -eq 5 ]; then
echo "Skipping $i"
continue
fi
echo "i = $i"
doneπ Output:
i = 1
i = 2
i = 3
i = 4
Skipping 5
i = 6
i = 7
n=0
while [ $n -lt 7 ]
do
((n++))
if [ $n -eq 3 ]; then
echo "Continue at $n"
continue
fi
if [ $n -eq 6 ]; then
echo "Break at $n"
break
fi
echo "n = $n"
donex=0
until [ $x -ge 10 ]
do
((x++))
if (( x == 4 )); then
echo "Skipping $x"
continue
fi
if (( x == 8 )); then
echo "Breaking at $x"
break
fi
echo "x = $x"
done- You can specify a numeric argument to break out of n levels of loops.
for i in {1..3}; do
for j in {1..3}; do
if [ $j -eq 2 ]; then
echo "Breaking both loops at i=$i, j=$j"
break 2 # exit both loops
fi
echo "i=$i, j=$j"
done
doneπ Without break 2, only the inner loop would stop.
πΉ 6. Real-Life Example (skip hidden files)
for file in *; do
if [[ $file == .* ]]; then
continue # skip hidden files
fi
echo "Processing: $file"
doneβ Summary:
breakβ exit the loop completelycontinueβ skip current iteration, go to nextbreak Nβ exit multiple nested loops
init=0
while [[ $init -lt 10 ]];do
((init++))
if [[ $init -eq 5 ]];then
continue
fi
echo ${init}
done
init=0; while [[ $init -lt 10 ]]; do ((init++)); if [[ $init -eq 5 ]]; then continue; fi; echo $init; done
Here is the exact **Bash code** from the image provided:
```bash
#!/bin/bash
initNumber=1
while [[ ${initNumber} -lt 3 ]]
do
for i in item1 item2 item3
do
echo "${initNumber} - ${i}"
if [[ $i == item2 ]]
then
break 2
fi
done
((initNumber++))
done
#!/bin/bash
initNumber=1
while [[ ${initNumber} -lt 3 ]]
do
for i in item1 item2 item3
do
echo "${initNumber} - ${i}"
if [[ $i == item2 ]]
then
break 2
fi
done
((initNumber++))
done
- Break normally breaks only one loop (the innermost one).
- Break N breaks out of N levels of loops.
- So break 2 breaks out of both the for loop and the outer while loop.