π 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:
πΉ What are Command Line Arguments?
When a script is executed, values passed after the script name are called command-line arguments.
Example:
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.
During each iteration:
creates folders such as:
πΉ Output
β 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.
πΉ 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.
πΉ Step-by-Step Explanation
Step 1
Initialize the variable:
Step 2
Check the condition:
The loop continues as long as the value of num is less than or equal to 10.
Step 3
Print the value:
Step 4
Increase the value by 2:
πΉ Output
β Result
The script prints all even numbers between 0 and 10.
π While Loop Example β Printing Odd Numbers
πΉ Step-by-Step Explanation
Step 1
Start from:
Step 2
Continue until:
Step 3
Print the current value:
Step 4
Increase by 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:
π 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.



