• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
DefenceDev
No Result
View All Result
Home Linux Bash Scripting

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

by neo
September 3, 2026
in Bash Scripting
0
Lesson 4: Bash Control Structures – loops (for, while, until)

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

0
SHARES
21
VIEWS
Share on FacebookShare on LinkedIn
Table of Contents
  • Lesson 4: Bash Control Structures – loops (for, while, until)
    • For Loop
      • Basic Syntax
    • While Loop
      • Basic Syntax
    • Until Loop
      • Basic syntax

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

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.

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
Tags: bashlinux
Previous Post

Lesson 3: Bash Control Structures – if else

Next Post

Lesson 5: Linux Bash Read User Input with Example

neo

Related Posts

Terminal Colors in Bash: How and When to Use Them
Bash Scripting

Terminal Colors in Bash: How and When to Use Them

Automating Linux Server Health Checks with a Bash Script
Bash Scripting

Automating Linux Server Health Checks with a Bash Script

Solution: Monitoring Disk Usage with Email Alert Using a Bash Script and Crontab on Linux
Bash Scripting

Disk Usage Monitoring with Bash and Cron

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting

Lesson 11: Practical Linux Bash Examples and Projects

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

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

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

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

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

Lesson 5: Linux Bash Read User Input with Example

Recommended

Fortinet FortiGate Firewall Policies Best Practices

FortiGate Firewall Policies Best Practices

MikroTik: Bridge Interface with Multiple Ports

MikroTik: Bridge Interface with Multiple Ports

Configure Telegram Notifications in Proxmox VE Using Webhooks

Fix Proxmox VM Snapshot “Out of Space” Errors

Fix Proxmox VM Snapshot “Out of Space” Errors

AI-generated illustration of a Proxmox cluster node rename and migration scenario.

Rename a Proxmox VE Node in a Cluster Without Reinstalling the Server

Categories

  • Linux
    • Bash Scripting
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Web Tools

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

Configure Telegram Notifications in Proxmox VE Using Webhooks

Fix Proxmox VM Snapshot “Out of Space” Errors

Fix Proxmox VM Snapshot “Out of Space” Errors

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

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools

© 2025 defencedev.com - All rights reserved.