Class 21 of 37 • Advance Shell Script \t\t\t\t

Looping Statements in Linux Shell Scripting - while, until & for Loops

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.

1. While Loop

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.

Syntax


while [ condition ]
do
   statement 1
   statement 2
done
							

Example


#!/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
							

2. Until Loop

The until loop is useful when you need to execute a set of commands until a condition is true.

Syntax


until [ condition ]
do
   statement 1
   statement 2
done
							

Example


#!/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
							

3. For Loop

a for loop is a bash programming language statement which allows code to be repeatedly executed

Syntax


for variable_name in item1 item2 ... itemN
do
    statement 1
    statement 2
done
							

Example (Range Syntax)


#!/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
							

Example (C-Style For Loop)

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
							

4. Reading a File Line-by-Line (While Loop Pattern)

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"
							

5. Loop Control: "break" and "continue"

     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
							

Student Practical Lab Programs (BCA University Exam)

Lab Program 1: Calculate Factorial of a Given Number


#!/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"
							

Lab Program 2: Generate Mathematical Multiplication Table


#!/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
							

Frequently Asked Questions (FAQ)

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.

Next Step in Your Linux Learning Path:

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

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!