ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.3 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.unixmen.com/bash-for-loop-a-complete-guide-for-perfect-scripts/ |
| Last Crawled | 2026-04-04 11:10:26 (8 days ago) |
| First Indexed | 2024-06-06 09:48:39 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | Bash For Loop: A Complete Guide for Perfect Scripts | Unixmen |
| Meta Description | Bash for loop—an extremely helpful control statement in Bash scripting—is just this article away from learning it to perfection. Dive in now! |
| Meta Canonical | null |
| Boilerpipe Text | Scripts have been helping people execute a lot of commands and perform complicated tasks like file operations. The next big step in scripting came in the form of automation, which made scripts even more helpful. Automation in Bash (Bourne Again SHell) command language scripts reduces human efforts and errors, improves efficiency, and maintains consistency. In this article, we will be learning about one important control flow statement: the Bash for loop.
A “for loop” in Bash scripting is a control flow statement that allows code to be executed repeatedly based on a list of items or a sequence of numbers. The syntax involves iterating over each item in a list and executing specified commands. Using for loops in Unix based systems is crucial for automation.
Some benefits of using a for loop in Bash are
Simplify repetitive tasks
Reduce human effort
Save plenty of time by executing tasks quickly
Cut down human errors via automation of processes.
What is a Bash For Loop?
In simple words, a Bash for loop is a control structure to repeatedly execute specified commands for all the items in a list. In a bash for loop, you have to specify a variable and a list or range to execute the commands on.
Bash for loop template
for “the item” in “the list”<br />do<br />“sample command 1”<br />“sample command 2”<br />…<br />done
Why Should You Use Bash for Loops?
Because it increases your efficiency and productivity exponentially. Multiple commands can be executed for each item in a list or sequence which drastically reduces the time and effort involved if the same has to be done manually. A lot of operations like system monitoring, data processing, and file processing can be done via bash for loops which adds one more reason why you should use bash for loops.
Here are some relatable examples to get you started in increasing levels of complexity. If you have noticed the script starting with #!/bin/bash, it is called a shebang. Shebang tells the device that this is a bash script and has to be run using Bash shell.
If you would like to print the file size of each file in a directory
!/bin/bash<br />DIRECTORY="/sample_directory_path_here"<br />for file in "$DIRECTORY"/*<br />do<br />echo "File: $(basename "$file") - Size: $(stat -c%s "$file") bytes"<br />done
The output will be:
File: file1.jpg - Size: 150 bytes<br />File: file2.png - Size: 300 bytes
If you would like to iterate over multiple directories and compress each file
!/bin/bash<br />PARENT_DIR="/sample_directory_path_here"<br />for dir in "$PARENT_DIR"/*<br />do<br />if [ -d "$dir" ]; then<br />echo "Processing directory: $(basename "$dir")"<br />for file in "$dir"/*<br />do<br />if [ -f "$file" ]; then<br />gzip "$file"<br />echo "Compressed: $(basename "$file")"<br />fi<br />done<br />fi<br />done
The output will be:
Processing directory: directory1<br />Compressed: image1.jpg<br />Compressed: password.txt<br />Processing directory: directory2<br />Compressed: image2.jpg<br />Compressed: textfile.txt
Off-by-one Error in Bash for Loop
This is a common mistake while using Bash for loops. The off-by-one error occurs when the loop iterates either one time more or one time less. To understand this better, if you would want to iterate a sequence from 1 to 10, you should use {1..5} and not {1..6}.
Recap and Other Ways to Polish Bash for Loops
Congratulations! You are now ready to continue your path in writing perfect for loops in Bash. Time to automate some mundane daily tasks. If you would like to be a bash for loops master, practice your scripting skills with nested loops.
Some references to help you further
Cheat sheet for Bash commands
(external link to RedHat’s official site)
Bash Scripting 101 – As simplified as possible
Bash script arguments tutorial
Bash While Loop Examples |
| Markdown | - [Home](https://www.unixmen.com/)
- [Linux distributions](https://www.unixmen.com/category/linux-distributions/)
- [Linux tutorials](https://www.unixmen.com/category/linux-tutorials/)
- [News](https://www.unixmen.com/category/news/)
- [Frequently Asked Questions](https://www.unixmen.com/category/frequently-asked-questions/)
- [Opensource](https://www.unixmen.com/category/opensource/)
- [Unix](https://www.unixmen.com/category/unix/)
- [Linux HowTo’s](https://www.unixmen.com/category/linux-howtos/)
- [Linux Distro’s](https://www.unixmen.com/category/linux-distros/)
- [Linux & Open Source News](https://www.unixmen.com/category/linux-open-source-news/)
- [Contact Us](https://www.unixmen.com/contact-us/)
Tuesday, March 3, 2026
- [About Us](https://www.unixmen.com/about-us/)
- [Advertising on Unixmen](https://www.unixmen.com/advertising/)
- [Become a Contributor](https://www.unixmen.com/work-for-us/)
- [Unixmen collaborated with Unixstickers](https://www.unixmen.com/unixmen-collaborated-with-unixstickers/)
- [Contact Us](https://www.unixmen.com/contact-us/)
[ Unixmen](https://www.unixmen.com/)
[](https://www.unixmen.com/) [](https://www.unixmen.com/)
- [Home](https://www.unixmen.com/)
- [Linux distributions](https://www.unixmen.com/category/linux-distributions/)
- [Linux tutorials](https://www.unixmen.com/category/linux-tutorials/)
- [News](https://www.unixmen.com/category/news/)
- [Frequently Asked Questions](https://www.unixmen.com/category/frequently-asked-questions/)
- [Opensource](https://www.unixmen.com/category/opensource/)
- [Unix](https://www.unixmen.com/category/unix/)
- [Linux HowTo’s](https://www.unixmen.com/category/linux-howtos/)
- [Linux Distro’s](https://www.unixmen.com/category/linux-distros/)
- [Linux & Open Source News](https://www.unixmen.com/category/linux-open-source-news/)
- [Contact Us](https://www.unixmen.com/contact-us/)
[Home](https://www.unixmen.com/) [Featured](https://www.unixmen.com/category/featured-2/ "View all posts in Featured") [Shell Scripting](https://www.unixmen.com/category/featured-2/shell-scripting/ "View all posts in Shell Scripting") Bash For Loop: A Complete Guide for Perfect Scripts
# Bash For Loop: A Complete Guide for Perfect Scripts
By
[Edwin](https://www.unixmen.com/author/0971762527/)
[Share on Facebook](https://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.unixmen.com%2Fbash-for-loop-a-complete-guide-for-perfect-scripts%2F)
[Tweet on Twitter](https://twitter.com/intent/tweet?text=Bash+For+Loop%3A+A+Complete+Guide+for+Perfect+Scripts&url=https%3A%2F%2Fwww.unixmen.com%2Fbash-for-loop-a-complete-guide-for-perfect-scripts%2F&via=unixmen)
- [tweet](https://twitter.com/share)
[](https://www.unixmen.com/wp-content/uploads/2024/05/bash-for-loop.png)


Scripts have been helping people execute a lot of commands and perform complicated tasks like file operations. The next big step in scripting came in the form of automation, which made scripts even more helpful. Automation in Bash (Bourne Again SHell) command language scripts reduces human efforts and errors, improves efficiency, and maintains consistency. In this article, we will be learning about one important control flow statement: the Bash for loop.
A “for loop” in Bash scripting is a control flow statement that allows code to be executed repeatedly based on a list of items or a sequence of numbers. The syntax involves iterating over each item in a list and executing specified commands. Using for loops in Unix based systems is crucial for automation.
Some benefits of using a for loop in Bash are
- Simplify repetitive tasks
- Reduce human effort
- Save plenty of time by executing tasks quickly
- Cut down human errors via automation of processes.
## What is a Bash For Loop?
In simple words, a Bash for loop is a control structure to repeatedly execute specified commands for all the items in a list. In a bash for loop, you have to specify a variable and a list or range to execute the commands on.
**Bash for loop template**
for “the item” in “the list”\<br /\>do\<br /\>“sample command 1”\<br /\>“sample command 2”\<br /\>…\<br /\>done
## Why Should You Use Bash for Loops?
Because it increases your efficiency and productivity exponentially. Multiple commands can be executed for each item in a list or sequence which drastically reduces the time and effort involved if the same has to be done manually. A lot of operations like system monitoring, data processing, and file processing can be done via bash for loops which adds one more reason why you should use bash for loops.
Here are some relatable examples to get you started in increasing levels of complexity. If you have noticed the script starting with \#!/bin/bash, it is called a shebang. Shebang tells the device that this is a bash script and has to be run using Bash shell.
### If you would like to print the file size of each file in a directory
!/bin/bash\<br /\>DIRECTORY="/sample\_directory\_path\_here"\<br /\>for file in "\$DIRECTORY"/\*\<br /\>do\<br /\>echo "File: \$(basename "\$file") - Size: \$(stat -c%s "\$file") bytes"\<br /\>done
The output will be:
File: file1.jpg - Size: 150 bytes\<br /\>File: file2.png - Size: 300 bytes
### If you would like to iterate over multiple directories and compress each file
!/bin/bash\<br /\>PARENT\_DIR="/sample\_directory\_path\_here"\<br /\>for dir in "\$PARENT\_DIR"/\*\<br /\>do\<br /\>if \[ -d "\$dir" \]; then\<br /\>echo "Processing directory: \$(basename "\$dir")"\<br /\>for file in "\$dir"/\*\<br /\>do\<br /\>if \[ -f "\$file" \]; then\<br /\>gzip "\$file"\<br /\>echo "Compressed: \$(basename "\$file")"\<br /\>fi\<br /\>done\<br /\>fi\<br /\>done
The output will be:
Processing directory: directory1\<br /\>Compressed: image1.jpg\<br /\>Compressed: password.txt\<br /\>Processing directory: directory2\<br /\>Compressed: image2.jpg\<br /\>Compressed: textfile.txt
## Off-by-one Error in Bash for Loop
This is a common mistake while using Bash for loops. The off-by-one error occurs when the loop iterates either one time more or one time less. To understand this better, if you would want to iterate a sequence from 1 to 10, you should use {1..5} and not {1..6}.
### Recap and Other Ways to Polish Bash for Loops
Congratulations! You are now ready to continue your path in writing perfect for loops in Bash. Time to automate some mundane daily tasks. If you would like to be a bash for loops master, practice your scripting skills with nested loops.
### Some references to help you further
- [Cheat sheet for Bash commands](https://developers.redhat.com/cheat-sheets/bash-shell-cheat-sheet) (external link to RedHat’s official site)
- [Bash Scripting 101 – As simplified as possible](https://www.unixmen.com/bash-scripting-basics/)
- [Bash script arguments tutorial](https://www.unixmen.com/bash-script-arguments-a-comprehensive-guide/)
- [Bash While Loop Examples](https://www.unixmen.com/bash-while-loop-examples-for-loops-until-loops-and-more/)
[Edwin](https://www.unixmen.com/author/0971762527/)
#### Latest Articles
[](https://www.unixmen.com/how-to-repair-mysql-database-in-linux/ "How to Repair MySQL Database in Linux?")
### [How to Repair MySQL Database in Linux?](https://www.unixmen.com/how-to-repair-mysql-database-in-linux/ "How to Repair MySQL Database in Linux?")
[Linux HowTo's](https://www.unixmen.com/category/linux-howtos/) [Janus Atienza](https://www.unixmen.com/author/landocsgp/) \-
February 18, 2026
[0](https://www.unixmen.com/how-to-repair-mysql-database-in-linux/#respond)
MySQL database files are prone to corruption and inconsistencies. They can easily get corrupted due to several reasons, such as virus attack on the...
[](https://www.unixmen.com/uses-of-linux-how-individuals-and-organizations-use-it/ "Uses of Linux: How Individuals and Organizations Use It")
### [Uses of Linux: How Individuals and Organizations Use It](https://www.unixmen.com/uses-of-linux-how-individuals-and-organizations-use-it/ "Uses of Linux: How Individuals and Organizations Use It")
[Linux HowTo's](https://www.unixmen.com/category/linux-howtos/) [Janus Atienza](https://www.unixmen.com/author/landocsgp/) \-
February 10, 2026
[0](https://www.unixmen.com/uses-of-linux-how-individuals-and-organizations-use-it/#respond)
Ever ask yourself why so many people talk about Linux and why it keeps showing up in phones, offices, and even at home? Linux...
[](https://www.unixmen.com/debian-hosting-is-not-trendy-and-thats-why-it-wins/ "Debian Hosting Is Not Trendy, and That’s Why It Wins")
### [Debian Hosting Is Not Trendy, and That’s Why It Wins](https://www.unixmen.com/debian-hosting-is-not-trendy-and-thats-why-it-wins/ "Debian Hosting Is Not Trendy, and That’s Why It Wins")
[Hosting](https://www.unixmen.com/category/hosting/) [Janus Atienza](https://www.unixmen.com/author/landocsgp/) \-
February 4, 2026
[0](https://www.unixmen.com/debian-hosting-is-not-trendy-and-thats-why-it-wins/#respond)
Hosting based on Debian OS is far from modernized control panels, AI-integration, and all other things new Linux distros are trying to hook you...
[](https://www.unixmen.com/)
ABOUT US
Unixmen provide Linux Howtos, Tutorials, Tips & Tricks ,Opensource News. It cover most popular distros like Ubuntu, LinuxMint, Fedora, Centos. It is your Gate to the the world of Linux/Unix and Opensource in General.
[Privacy Policy](https://ads.landocsventures.com/unixmen/Privacy/Privacy.pdf) [Cookie Policy](https://ads.landocsventures.com/unixmen/Privacy/COOKIE.pdf) [Advertise](https://www.unixmen.com/advertising/)
© Copyright 2020 - Newspaper 6 by TagDiv |
| Readable Markdown | [](https://www.unixmen.com/wp-content/uploads/2024/05/bash-for-loop.png)

Scripts have been helping people execute a lot of commands and perform complicated tasks like file operations. The next big step in scripting came in the form of automation, which made scripts even more helpful. Automation in Bash (Bourne Again SHell) command language scripts reduces human efforts and errors, improves efficiency, and maintains consistency. In this article, we will be learning about one important control flow statement: the Bash for loop.
A “for loop” in Bash scripting is a control flow statement that allows code to be executed repeatedly based on a list of items or a sequence of numbers. The syntax involves iterating over each item in a list and executing specified commands. Using for loops in Unix based systems is crucial for automation.
Some benefits of using a for loop in Bash are
- Simplify repetitive tasks
- Reduce human effort
- Save plenty of time by executing tasks quickly
- Cut down human errors via automation of processes.
## What is a Bash For Loop?
In simple words, a Bash for loop is a control structure to repeatedly execute specified commands for all the items in a list. In a bash for loop, you have to specify a variable and a list or range to execute the commands on.
**Bash for loop template**
for “the item” in “the list”\<br /\>do\<br /\>“sample command 1”\<br /\>“sample command 2”\<br /\>…\<br /\>done
## Why Should You Use Bash for Loops?
Because it increases your efficiency and productivity exponentially. Multiple commands can be executed for each item in a list or sequence which drastically reduces the time and effort involved if the same has to be done manually. A lot of operations like system monitoring, data processing, and file processing can be done via bash for loops which adds one more reason why you should use bash for loops.
Here are some relatable examples to get you started in increasing levels of complexity. If you have noticed the script starting with \#!/bin/bash, it is called a shebang. Shebang tells the device that this is a bash script and has to be run using Bash shell.
### If you would like to print the file size of each file in a directory
!/bin/bash\<br /\>DIRECTORY="/sample\_directory\_path\_here"\<br /\>for file in "\$DIRECTORY"/\*\<br /\>do\<br /\>echo "File: \$(basename "\$file") - Size: \$(stat -c%s "\$file") bytes"\<br /\>done
The output will be:
File: file1.jpg - Size: 150 bytes\<br /\>File: file2.png - Size: 300 bytes
### If you would like to iterate over multiple directories and compress each file
!/bin/bash\<br /\>PARENT\_DIR="/sample\_directory\_path\_here"\<br /\>for dir in "\$PARENT\_DIR"/\*\<br /\>do\<br /\>if \[ -d "\$dir" \]; then\<br /\>echo "Processing directory: \$(basename "\$dir")"\<br /\>for file in "\$dir"/\*\<br /\>do\<br /\>if \[ -f "\$file" \]; then\<br /\>gzip "\$file"\<br /\>echo "Compressed: \$(basename "\$file")"\<br /\>fi\<br /\>done\<br /\>fi\<br /\>done
The output will be:
Processing directory: directory1\<br /\>Compressed: image1.jpg\<br /\>Compressed: password.txt\<br /\>Processing directory: directory2\<br /\>Compressed: image2.jpg\<br /\>Compressed: textfile.txt
## Off-by-one Error in Bash for Loop
This is a common mistake while using Bash for loops. The off-by-one error occurs when the loop iterates either one time more or one time less. To understand this better, if you would want to iterate a sequence from 1 to 10, you should use {1..5} and not {1..6}.
### Recap and Other Ways to Polish Bash for Loops
Congratulations! You are now ready to continue your path in writing perfect for loops in Bash. Time to automate some mundane daily tasks. If you would like to be a bash for loops master, practice your scripting skills with nested loops.
### Some references to help you further
- [Cheat sheet for Bash commands](https://developers.redhat.com/cheat-sheets/bash-shell-cheat-sheet) (external link to RedHat’s official site)
- [Bash Scripting 101 – As simplified as possible](https://www.unixmen.com/bash-scripting-basics/)
- [Bash script arguments tutorial](https://www.unixmen.com/bash-script-arguments-a-comprehensive-guide/)
- [Bash While Loop Examples](https://www.unixmen.com/bash-while-loop-examples-for-loops-until-loops-and-more/) |
| Shard | 67 (laksa) |
| Root Hash | 12996476488780439067 |
| Unparsed URL | com,unixmen!www,/bash-for-loop-a-complete-guide-for-perfect-scripts/ s443 |