• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to use for loop in Bash Script on Debian 12

  • 00:37 cat /etc/os-release
  • 00:56 vim for_loop
  • 04:03 chmod +x for_loop
  • 04:14 ls -la
  • 04:25 ./for_loop
  • 04:47 ls -la
{{postValue.id}}

To Use For Loop In Bash Script On Debian 12

Introduction:

Similar to other programming languages, Bash shell scripting offers support for ‘for loops’ to accomplish iterative tasks. It allows us to iterate through a specific set of statements across a range of words in a string or elements in an array.

Procedure Steps:

Step 1: Check the OS version by using following command.

root@linuxhelp:~/linuxhelp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Create a script file using for loop by using following command.

root@linuxhelp:~/linuxhelp# vim for_loop
#!/bin/bash
read i
for name in txt{1..7}
do
	echo "The .txt files will be creating from 1 to 7 while running the script"
	touch $i.$name
	name=$(($name+1))
	sleep 1
done

Step 3: Make the executable permission by using following command.

root@linuxhelp:~/linuxhelp# chmod +x for_loop

Step 4: Long list the files to check the executable permission by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  7 05:05 .
drwx------ 8 root root 4096 Dec  7 05:05 ..
-rwxr-xr-x 1 root root  173 Dec  7 05:05 for_loop

Step 5: Run the script file by using following command.

root@linuxhelp:~/linuxhelp# ./for_loop 
test
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script
The .txt files will be creating from 1 to 7 while running the script

Step 6: Long list the files to check the created files by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  7 05:06 .
drwx------ 8 root root 4096 Dec  7 05:05 ..
-rwxr-xr-x 1 root root  173 Dec  7 05:05 for_loop
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt1
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt2
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt3
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt4
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt5
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt6
-rw-r--r-- 1 root root    0 Dec  7 05:06 test.txt7

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use for loop in Bash Script on Debian 12. Your feedback is much welcome.

Tags:
liam
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

Basic Syntax: Use the `for` loop with the following syntax:

A

for variable in the list
do
# commands to be executed
done

Q

Numeric Range: For a numeric range, use `{start..end}`. Example:

A

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

Q

Iterating Over an Array: Use `for element in "${array[@]}"`. Example:

A

fruits=("Apple" "Banana" "Orange")
for fruit in "${fruits[@]}"
do
echo $fruit
done

Q

Command Substitution: Iterate over the output of a command. Example:

A

for file in $(ls *.txt)
do
echo $file
done

Q

Using Sequence: Use `seq` for a numeric sequence. Example:

A

for i in $(seq 1 2 10)
do
echo $i
done

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Ryan ?
how to use visual traceroute tool

Am using traceroute command to check for the route. i got this tool while surfing. So pls help me out installation and usage of Visual traceroute tool.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.