Loops in Shell Scripting
Loops allow a shell script to execute a command or a group of commands repeatedly without writing the same instructions multiple times. They are especially useful in Linux automation, where repetitive tasks such as processing files, checking services, or executing commands need to be performed efficiently.
Shell scripting commonly uses two types of loops:
| Loop | Purpose |
|---|---|
for | Repeats commands over a defined range or a list of values. |
while | Repeats commands as long as a condition remains true. |
The for Loop
A for loop is useful when you know how many times a command needs to execute or want to iterate over a sequence of values.
Syntax
for (( initialization; condition; update ))
do
# Commands to execute
doneThe three parts inside (( )) control the loop:
| Part | Purpose | Example |
|---|---|---|
| Initialization | Sets the starting value. | i=1 |
| Condition | Determines whether the loop continues. | i<=4 |
| Update | Changes the variable after each iteration. | i++ |
Example: Print a Message Four Times
#!/bin/bash
for (( i=1; i<=4; i++ ))
do
echo "Focus is the key to learn any skill"
doneOutput:
Focus is the key to learn any skill
Focus is the key to learn any skill
Focus is the key to learn any skill
Focus is the key to learn any skillHow It Works
- The variable
istarts at1. - The condition
i<=4is checked. - If the condition is true, the loop body executes.
- The value of
iincreases by1. - The condition is checked again.
- When
ibecomes5, the condition is false, and the loop stops.
| Iteration | Value of i | Condition | Action |
|---|---|---|---|
| 1 | 1 | True | Execute |
| 2 | 2 | True | Execute |
| 3 | 3 | True | Execute |
| 4 | 4 | True | Execute |
| 5 | 5 | False | Stop |
The loop executes four times, even though the command is written only once.
The while Loop
A while loop executes a block of commands repeatedly as long as a specified condition remains true.
It is useful when the number of iterations is not known in advance and depends on a condition.
Syntax
while [ condition ]
do
# Commands to execute
doneExample: Print Numbers from 1 to 5
#!/bin/bash
num=1
while [ "$num" -le 5 ]
do
echo "$num"
((num++))
doneOutput:
1
2
3
4
5How It Works
- The variable
numstarts at1. - The condition
num -le 5is checked. - If true, the current value is printed.
- The variable increases by
1. - The condition is checked again.
- When
numbecomes6, the condition is false, and the loop terminates.
| Iteration | Value of num | Condition | Action |
|---|---|---|---|
| 1 | 1 | True | Prints 1 |
| 2 | 2 | True | Prints 2 |
| 3 | 3 | True | Prints 3 |
| 4 | 4 | True | Prints 4 |
| 5 | 5 | True | Prints 5 |
| 6 | 6 | False | Stop |
Important: Unlike the for loop, the while loop does not automatically update the variable. You must update it inside the loop whenever the condition depends on that variable.
Infinite Loops
An infinite loop occurs when a loop's condition never becomes false. This can happen when the variable controlling a while loop is not updated correctly.
Example
#!/bin/bash
num=1
while [ "$num" -le 5 ]
do
echo "$num"
# Missing variable update
doneIn this example:
numstarts at1.- The condition
num -le 5remains true. - Since
numis never updated, the loop keeps printing1.
The loop continues until it is stopped manually or the process is terminated.
How to stop it: Press Ctrl + C in the terminal to interrupt the running script.
Always ensure that a condition-based loop has a way to reach its termination condition, unless an infinite loop is intentional.
for vs while Loop

Quick Syntax
| Element | for Loop | while Loop |
|---|---|---|
| Starting keyword | for (( ... )) | while [ condition ] |
| Body starts with | do | do |
| Body ends with | done | done |
| Variable update | Usually inside the loop declaration | Usually inside the loop body |
Summary
- Loops automate repetitive tasks by executing commands multiple times.
forloop is suitable when iterating over a known range, count, or list of values.whileloop continues executing as long as its condition remains true.- The
forloop typically handles initialization, condition checking, and updating in its declaration. - A
whileloop requires you to manage variable updates when necessary. - An incorrectly managed loop can become infinite. Use
Ctrl + Cto interrupt it from the terminal.
Loops are fundamental to shell scripting because they make repetitive Linux tasks easier to automate, maintain, and execute efficiently.
Written By: Muskan Garg
How is this guide?
Last updated on
