How To

How To Use Bash For Loop With Practical Examples

Introduction

A Bash for loop is used to repeat a command or a group of commands multiple times. If you are learning Bash scripting, loops are very important because they help you automate repetitive tasks. Instead of writing the same command again and again, you can use a for loop to run it automatically.

Bash for loops are commonly used for file handling, user management, log analysis, backups, cybersecurity automation, and system administration. For example, you can loop through files in a directory, scan multiple IP addresses, rename files, or check several services at once.

What Is A For Loop In Bash?

A for loop runs commands for each item in a list.

Basic syntax:

for item in listdo    commanddone

You can also write it in one line:

for item in list; do command; done

The loop starts with for and ends with done.

Basic Bash For Loop Example

Create a new Bash script:

nano for-loop.sh

Add the following code:

#!/bin/bashfor name in Kali Ubuntu Debian Fedorado    echo "Linux distribution: $name"done

Save the file and run it:

chmod +x for-loop.sh./for-loop.sh

Output:

Linux distribution: KaliLinux distribution: UbuntuLinux distribution: DebianLinux distribution: Fedora

In this example, the variable name stores each item one by one.

Bash For Loop With Numbers

You can use a for loop with numbers using brace expansion.

#!/bin/bashfor number in {1..5}do    echo "Number: $number"done

Output:

Number: 1Number: 2Number: 3Number: 4Number: 5

This is useful when you want to repeat a command a fixed number of times.

Bash For Loop With Files

A common use of for loops is to process files in a directory.

#!/bin/bashfor file in *.txtdo    echo "Found text file: $file"done

This script checks all .txt files in the current directory and prints their names.

Rename Multiple Files Using For Loop

You can also rename multiple files using a loop.

#!/bin/bashfor file in *.logdo    mv "$file" "backup-$file"done

This script adds backup- before every .log file name.

Cybersecurity Example: Loop Through IP Addresses

For cybersecurity learners, loops are useful for scanning multiple hosts.

#!/bin/bashfor ip in 192.168.1.{1..5}do    ping -c 1 $ipdone

This script pings IP addresses from 192.168.1.1 to 192.168.1.5.

Conclusion

The Bash for loop is a powerful feature for automation. It helps you repeat commands, process files, handle numbers, and automate Linux tasks easily.

For beginners, learning for loops is an important step in Bash scripting. Once you understand this concept, you can create better scripts for file management, backups, system monitoring, cybersecurity checks, and Linux automation.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago