π Linux Shell Scripting: Understanding for and while Loops with Practical Examples

Shell scripting becomes powerful when combined with loops. Loops help automate repetitive tasks such as creating files, generating folders, and printing sequences of numbers. In this article, we'll explore For Loops and While Loops with practical examples commonly used in Linux Administration.
πFor Loop - Understanding Command Line Arguments (\(1, \)2, $3)
Consider the following script:
#!/bin/bash
for (( num=\(2; num<=\)3; num++ ))
do
mkdir "\(1\)num"
done
πΉ What are Command Line Arguments?
When a script is executed, values passed after the script name are called command-line arguments.
Example:
bash certain.sh Day 1 50
Here:
| Argument | Value |
|---|---|
$1 |
Day |
$2 |
1 |
$3 |
50 |
πΉ How the Script Works
The loop starts from the value stored in \(2 and continues until the value stored in \)3.
for (( num=\(2; num<=\)3; num++ ))
During each iteration:
mkdir "\(1\)num"
creates folders such as:
Day1
Day2
Day3
...
Day50
πΉ Output
Day1
Day2
Day3
...
Day50
β Key Point
This script is useful when you want to automate folder creation using values supplied directly while running the script.
π Creating Files Using User Input (read Command)
Instead of supplying arguments during execution, we can ask the user to provide the required information.
#!/bin/bash
read -p "Enter the name of file: " fol
read -p "Enter the start range: " start
read -p "Enter the end range: " end
for (( num=\(start; num<=\)end; num++ ))
do
touch "\(fol\)num"
done
πΉ How This Script Works
The script asks the user to enter:
File name prefix
Starting range
Ending range
πΉ Output
πΉ Why Use read?
The read command makes scripts interactive by accepting values directly from the user during runtime.
β Benefits
User-friendly
Flexible
No need to pass arguments manually
π Difference Between the Two for Loop Programs
| Using Arguments | Using User Input |
|---|---|
Uses \(1, \)2, $3 |
Uses variables from read |
| Faster for automation | More interactive |
| Best for scheduled scripts | Best for beginners and manual execution |
π While Loop Example β Printing Even Numbers
A while loop executes repeatedly as long as the specified condition remains true.
#!/bin/bash
num=0
while (( num <= 10 ))
do
echo $num
(( num += 2 ))
done
πΉ Step-by-Step Explanation
Step 1
Initialize the variable:
num=0
Step 2
Check the condition:
while (( num <= 10 ))
The loop continues as long as the value of num is less than or equal to 10.
Step 3
Print the value:
echo $num
Step 4
Increase the value by 2:
(( num += 2 ))
πΉ Output
β Result
The script prints all even numbers between 0 and 10.
π While Loop Example β Printing Odd Numbers
#!/bin/bash
num=1
while (( num <= 10 ))
do
echo $num
(( num += 2 ))
done
πΉ Step-by-Step Explanation
Step 1
Start from:
num=1
Step 2
Continue until:
num <= 10
Step 3
Print the current value:
echo $num
Step 4
Increase by 2:
(( num += 2 ))
πΉ Output
β Result
The script prints all odd numbers between 1 and 10.
π When Should You Use a while Loop?
Use a while loop when:
β Execution depends on a condition.
β The number of iterations is unknown.
β You need continuous checking or validation.
Example:
while (( num <= 100 ))
π Comparison: for Loop vs while Loop
| Feature | For Loop | While Loop |
|---|---|---|
| Iterations Known | β Yes | β Not Required |
| Condition Based | Limited | β Yes |
| Easy for Ranges | β Excellent | Good |
| User Input Handling | Good | β Excellent |
| Common Use Cases | File/Folder Creation | Counting & Validation |
π― Conclusion
Loops are one of the most important building blocks of Shell Scripting.
πΉ for Loop
Ideal for fixed ranges.
Commonly used for creating files and folders.
Supports command-line arguments and user input.
πΉ while Loop
Executes based on a condition.
Useful for printing sequences and validation tasks.
Commonly used in Linux Administration scripts.
By understanding these looping techniques, you can automate repetitive Linux tasks efficiently and build a strong foundation in Shell Scripting and Linux Administration.



