loop is a bash programming language statement which allows code to be repeatedly executed. A loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.
The while loop enables you to execute set of commands repeatedly until some condition occurs.it usually used when you need to manipulate the value of a variable repeatedly.
while [ condition ]
do
statement 1
statement 2
done
#!/bin/bash
# Print numbers from 1 to 10 using while loop
i=1
while [ "$i" -le 10 ]
do
echo "Number: $i"
# Increment counter (no spaces around = in assignment)
i=$(( i + 1 ))
done
The until loop is useful when you need to execute a set of commands until a condition is true.
until [ condition ]
do
statement 1
statement 2
done
#!/bin/bash
# Print numbers from 1 to 5 using until loop (stops when i becomes > 5)
i=1
until [ "$i" -gt 5 ]
do
echo "Value: $i"
i=$(( i + 1 ))
done
a for loop is a bash programming language statement which allows code to be repeatedly executed
for variable_name in item1 item2 ... itemN
do
statement 1
statement 2
done
#!/bin/bash
# Iterate through range from 1 to 10
for no in {1..10}
do
echo "Count: $no"
done
# Range with step (e.g. odd numbers 1 to 9)
for odd in {1..10..2}
do
echo "Odd Number: $odd"
done
Bash also supports the three-expression C-language style for (( ... )) loop, which is very popular among C and BCA students:
#!/bin/bash
# C-style for loop in Bash
for (( i=1; i<=5; i++ ))
do
echo "Iteration: $i"
done
One of the most important real-world uses of loops in Linux is processing text files or log files line-by-line using while read:
#!/bin/bash
# Safely process each line of a text file
filename="/etc/resolv.conf"
while IFS= read -r line
do
echo "Config Line: $line"
done < "$filename"
break: Immediately terminates the current loop and transfers control to the statement following done.
continue: Skips the remaining commands in the current iteration and jumps immediately to the next loop cycle.
#!/bin/bash
# Demonstration of break and continue
for n in {1..10}
do
if [ "$n" -eq 3 ]; then
echo "Skipping number 3 (continue)"
continue
fi
if [ "$n" -eq 7 ]; then
echo "Hit number 7, exiting loop early (break)"
break
fi
echo "Current: $n"
done
#!/bin/bash
# Calculate factorial of N: N! = N * (N-1) * ... * 1
echo -n "Enter a positive integer: "
read n
fact=1
for (( i=1; i<=n; i++ ))
do
fact=$(( fact * i ))
done
echo "Factorial of $n is = $fact"
#!/bin/bash
# Multiplication table generator
echo -n "Enter table number: "
read num
echo "=== Multiplication Table of $num ==="
for i in {1..10}
do
res=$(( num * i ))
echo "$num x $i = $res"
done
An infinite loop can be written using while true; do ... done or while :; do ... done (where : is the built-in null command that always returns exit status 0). Press Ctrl + C to interrupt and terminate an infinite loop.
Use filename globbing and always quote the loop variable: for file in *.txt; do [ -e "$file" ] || continue; echo "Processing: $file"; done. Avoid using for file in $(ls) because word splitting breaks files containing spaces.
Store and manipulate indexed lists and associative hash maps in Class 22: Arrays in Shell Scripting.
Continue to Class 22: Arrays in Shell Scripting → Previous: ← Conditionals | Full Syllabus