• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Bash Scripting Tutorials

Lesson 4: Bash Control Structures – loops (for, while, until)

neo by neo
August 2, 2025
in Bash Scripting Tutorials
0
Lesson 4: Bash Control Structures – loops (for, while, until)

Lesson 4: Bash Control Structures – loops (for, while, until)

0
SHARES
3
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Lesson 4: Bash Control Structures – loops (for, while, until)

Welcome to Lesson 4: Bash Control Structures – Loops (for, while, until). Loops are a powerful feature in Bash scripting that allow you to perform repetitive tasks efficiently. In this lesson, we’ll explore the three main types of loops: for, while, and until. You’ll learn how to use these loops to iterate over lists, process files, and manage complex tasks with minimal code. By the end, you’ll understand how to apply loops to automate repetitive actions and make your scripts more efficient. Let’s get started with mastering loops in Bash!

For Loop

The for loop is used to iterate over a list of items. It is particularly useful when you know the number of iterations in advance. Here’s the basic syntax:

Basic Syntax

for item in list
do
    # Commands to execute
done
  • item: A placeholder that stores the current value from the list during each iteration.
  • list: A sequence of items to iterate over. It can be a static list, a range of numbers, or output from a command.
  • Commands: These are executed in each iteration of the loop.

Example

for i in {1..5}
do
    echo "Iteration $i"
done

Output

ADVERTISEMENT
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

While Loop

The while loop continues to execute as long as a specified condition is true. It is useful when the number of iterations is not known beforehand.

Basic Syntax

while [ condition ]
do
    # Commands to execute
done
  • condition: A logical test that determines if the loop continues. The condition can be any command or expression that returns a status of 0 (true).
  • Commands: The operations performed on each iteration while the condition remains true.

Example

count=1
while [ $count -le 5 ]
do
    echo "Count is $count"
    ((count++))
done

This loop will print the count from 1 to 5.

Until Loop

The until loop is similar to the while loop, but it continues to execute as long as the specified condition is false.

Basic syntax

until [ condition ]
do
    # Commands to execute
done
  • condition: A logical test that determines if the loop should stop. The condition is evaluated at the beginning of each iteration.
  • Commands: The block of code that executes while the condition remains false.

Example

count=1
until [ $count -gt 5 ]
do
    echo "Count is $count"
    ((count++))
done

This loop will also print the count from 1 to 5.

ADVERTISEMENT
ADVERTISEMENT

Lesson 4: Bash Control Structures – loops (for, while, until)

defencedev.com


«Previous: Lesson 3: Bash Control Structures – if else
Next: Lesson 5: Linux Bash Read User Input with Example

About The Author

neo

See author's posts

ADVERTISEMENT
Previous Post

Lesson 3: Bash Control Structures – if else

Next Post

Lesson 5: Linux Bash Read User Input with Example

neo

neo

Related Posts

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting Tutorials

Lesson 11: Practical Linux Bash Examples and Projects

Lesson 10: Bash File Operation (Read, Write, Copy)
Bash Scripting Tutorials

Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples
Bash Scripting Tutorials

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Lesson 8: Working with Bash Strings
Bash Scripting Tutorials

Lesson 8: Working with Bash Strings

Lesson 7: Bash Functions with Examples
Bash Scripting Tutorials

Lesson 7: Bash Functions with Examples

Lesson 6: Using Bash Command Substitution
Bash Scripting Tutorials

Lesson 6: Using Bash Command Substitution

Next Post
Lesson 5: Linux Bash Read User Input with Example

Lesson 5: Linux Bash Read User Input with Example

Recommended

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

Lesson 6: Using Bash Command Substitution

Lesson 6: Using Bash Command Substitution

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

GitHub: How to Add a Script or a Folder to Your Repository

GitHub: How to Add a Script or a Folder to Your Repository

Categories

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos
ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.