Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’4 min read
πŸš€ 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.