âšď¸ 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 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.linuxfordevices.com/tutorials/shell-script/for-loop-in-shell-scripts |
| Last Crawled | 2026-04-07 00:27:41 (18 hours ago) |
| First Indexed | 2020-03-11 12:11:41 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Shell Scripting 101: The for Loop in Shell Scripts - LinuxForDevices |
| Meta Description | Moving ahead with our tutorials now, let's introduce the for loop in shell scripts in our today's topic. We've already covered the while loop in our previous |
| Meta Canonical | null |
| Boilerpipe Text | Moving ahead with our tutorials now, letâs introduce the for loop in shell scripts in our todayâs topic. Weâve already covered the
while loop
in our previous tutorial and used the
break and continue statements
to quit the while loop based on certain conditions. Letâs get right into the topic then!
Whatâs The Difference Between while Loop and for Loop in Shell Scripts?
The major difference between for loop and while loop is in their definition and syntax.
When working with the while loop, we provide one condition that the while loop continues to loop on until it turns false. We do not specify the number of iterations for the loop. On the other hand, for loop takes 2 or 3 conditions and the last condition usually decides the number of iterations.
The first condition is the variable that iterates through a sequence when the loop begins and increments automatically based on what itâs iterating over. For example, if it has to run over a sequence of numbers, the variable increments itself based on the numbers supplied to it.
If everything I wrote above sounded like gibberish to you, just continue reading and everything will become crystal clear soon.
Definition of the Bash for loop
There are two ways you can define the bash for loop. The first is the bash style with two variables, and the second is C style with 3 variables. Hereâs the syntax for both of them.
Bash Style for loop definition:
for VARIABLE in SEQUENCE
The
VARIABLE
is the variable name to use within the loop, and the
SEQUENCE
is anything ranging from a sequence of numbers, multiple files, Linux commands, etc.
C Style for loop definition:
for (( variable declaration; condition; increment variable))
How to Use the for Loop?
Now that you have a basic understanding of the
for loop
, itâs time to start using it in our scripts. Weâll start by creating a simple for loop that iterates over a given sequence of numbers.
#!/bin/bash
for i in 1 2 3 4 5 6 7
do
echo "Loop number $i"
done
For Loop In Shell Scripts
Very simple to understand? Letâs move on to a different sequence group.
Bash For Loop Over Files
The for loop doesnât read the contents of the file when we loop over it but just allows us to perform operations on individual files while looping over them. Letâs see an example below.
#!/bin/bash
for i in *.txt
do
echo "Contents from the files: $i"
cat $i
done
For Loop Iterate Over Files
Iâve created two files with some text in them for demonstration purposes. See how I write the
cat command
once and the for loop iterates over both the files.
Writing C Style for Loops in Shell Scripts
You should be comfortable with iterating over individual sequences now. Letâs move on to the C style for loops that we talked about before. If youâve already worked with C or C++, this is going to be almost the same thing. But even if you havenât itâs not too difficult.
Letâs create a for loop that iterates 7 times and echos the number.
for (( i=1; i<=7; i++ ))
do
echo "Loop number $i"
done
C Style For Loop
Letâs understand the loop section by section. The reason why we use the double parenthesis is to signal bash that weâre using a C style expression within the brackets.
The first expression:
i=1
creates the start of the loop.
The second expression:
i<=7
tells that the loop should run until the variable is lesser than or equal to 7
The last expression:
i++
decides how the increment happens. In this case, it happens by 1
Conclusion
I hope this helps you understand how the for loop in shell scripts works. Feel free to comment below for any questions that you may have. |
| Markdown | [Skip to content](https://www.linuxfordevices.com/tutorials/shell-script/for-loop-in-shell-scripts#content)
[](https://www.linuxfordevices.com/)
Main Menu
- [Home](https://www.linuxfordevices.com/)
- [About](https://www.linuxfordevices.com/about)
- [Contact Us](https://www.linuxfordevices.com/contact-me)
- [Privacy Policy](https://www.linuxfordevices.com/privacy-policy-2)
# Shell Scripting 101: The for Loop in Shell Scripts
By [Ninad Pathak](https://www.linuxfordevices.com/author/ninad "View all posts by Ninad Pathak") / March 3, 2020

Moving ahead with our tutorials now, letâs introduce the for loop in shell scripts in our todayâs topic. Weâve already covered the [while loop](https://www.linuxfordevices.com/tutorials/shell-script/while-loop-in-shell-script) in our previous tutorial and used the [break and continue statements](https://www.linuxfordevices.com/tutorials/shell-script/break-and-continue-statement-in-linux-shell-script) to quit the while loop based on certain conditions. Letâs get right into the topic then\!
## Whatâs The Difference Between while Loop and for Loop in Shell Scripts?
**The major difference between for loop and while loop is in their definition and syntax.** When working with the while loop, we provide one condition that the while loop continues to loop on until it turns false. We do not specify the number of iterations for the loop. On the other hand, for loop takes 2 or 3 conditions and the last condition usually decides the number of iterations.
The first condition is the variable that iterates through a sequence when the loop begins and increments automatically based on what itâs iterating over. For example, if it has to run over a sequence of numbers, the variable increments itself based on the numbers supplied to it.
If everything I wrote above sounded like gibberish to you, just continue reading and everything will become crystal clear soon.
## Definition of the Bash for loop
There are two ways you can define the bash for loop. The first is the bash style with two variables, and the second is C style with 3 variables. Hereâs the syntax for both of them.
**Bash Style for loop definition:**
```
for VARIABLE in SEQUENCE
```
The **VARIABLE** is the variable name to use within the loop, and the **SEQUENCE** is anything ranging from a sequence of numbers, multiple files, Linux commands, etc.
**C Style for loop definition:**
```
for (( variable declaration; condition; increment variable))
```
## How to Use the for Loop?
Now that you have a basic understanding of the **for loop**, itâs time to start using it in our scripts. Weâll start by creating a simple for loop that iterates over a given sequence of numbers.
```
#!/bin/bash
for i in 1 2 3 4 5 6 7
do
echo "Loop number $i"
done
```

For Loop In Shell Scripts
Very simple to understand? Letâs move on to a different sequence group.
### Bash For Loop Over Files
The for loop doesnât read the contents of the file when we loop over it but just allows us to perform operations on individual files while looping over them. Letâs see an example below.
```
#!/bin/bash
for i in *.txt
do
echo "Contents from the files: $i"
cat $i
done
```

For Loop Iterate Over Files
Iâve created two files with some text in them for demonstration purposes. See how I write the [cat command](https://www.linuxfordevices.com/tutorials/linux/cat-more-command-in-linux) once and the for loop iterates over both the files.
### Writing C Style for Loops in Shell Scripts
You should be comfortable with iterating over individual sequences now. Letâs move on to the C style for loops that we talked about before. If youâve already worked with C or C++, this is going to be almost the same thing. But even if you havenât itâs not too difficult.
Letâs create a for loop that iterates 7 times and echos the number.
```
for (( i=1; i<=7; i++ ))
do
echo "Loop number $i"
done
```

C Style For Loop
Letâs understand the loop section by section. The reason why we use the double parenthesis is to signal bash that weâre using a C style expression within the brackets.
- The first expression: **i=1** creates the start of the loop.
- The second expression: **i\<=7** tells that the loop should run until the variable is lesser than or equal to 7
- The last expression: **i++** decides how the increment happens. In this case, it happens by 1
## Conclusion
I hope this helps you understand how the for loop in shell scripts works. Feel free to comment below for any questions that you may have.
[â Previous Post](https://www.linuxfordevices.com/tutorials/shell-script/if-else-in-shell-script "Shell Scripting 101: If Else in Shell Script")
[Next Post â](https://www.linuxfordevices.com/tutorials/shell-script/switch-case-in-shell-scripts "Shell Scripting 101: Switch Case in Shell Scripts")
## Recent Posts
- [How to Install Kali Linux in VirtualBox: Complete Setup Guide](https://www.linuxfordevices.com/tutorials/kali-linux/install-kali-linux-in-virtualbox)
- [Kali Linux Commands: Essential Guide for Penetration Testing](https://www.linuxfordevices.com/tutorials/kali-linux/kali-linux-commands)
- [Types of Shell in Linux: Understanding Your Command Line Options](https://www.linuxfordevices.com/tutorials/linux/types-of-shell-in-linux)
- [The Linux wget Command â Linux Download Command](https://www.linuxfordevices.com/tutorials/linux/linux-wget-command)
- [How to Install LM Studio â Step-by-Step Guide (2025)](https://www.linuxfordevices.com/tutorials/how-to-install-lm-studio-step-by-step-guide-2025)
## Favorite Sites
- [Python Tutorials](https://www.askpython.com/)
- [GoLang Tutorials](https://golangdocs.com/)
- [CodeForGeek](https://codeforgeek.com/)
- [VM-Help](https://www.vm-help.com/)
- [MySQL Tutorials](https://mysqlcode.com/)
- [Excel Tutorials](https://quickexcel.com/)

Learn everything about Linux, under a single roof. LinuxForDevices covers Linux tutorials, Open Source news, and courses to help you grow.
Copyright Š 2026 ¡ LinuxForDevices ¡ LinuxForDevices is part of Diyansh IT Services Private Limited
LinuxÂŽ is a registered trademark of Linus Torvalds. |
| Readable Markdown | Moving ahead with our tutorials now, letâs introduce the for loop in shell scripts in our todayâs topic. Weâve already covered the [while loop](https://www.linuxfordevices.com/tutorials/shell-script/while-loop-in-shell-script) in our previous tutorial and used the [break and continue statements](https://www.linuxfordevices.com/tutorials/shell-script/break-and-continue-statement-in-linux-shell-script) to quit the while loop based on certain conditions. Letâs get right into the topic then\!
## Whatâs The Difference Between while Loop and for Loop in Shell Scripts?
**The major difference between for loop and while loop is in their definition and syntax.** When working with the while loop, we provide one condition that the while loop continues to loop on until it turns false. We do not specify the number of iterations for the loop. On the other hand, for loop takes 2 or 3 conditions and the last condition usually decides the number of iterations.
The first condition is the variable that iterates through a sequence when the loop begins and increments automatically based on what itâs iterating over. For example, if it has to run over a sequence of numbers, the variable increments itself based on the numbers supplied to it.
If everything I wrote above sounded like gibberish to you, just continue reading and everything will become crystal clear soon.
## Definition of the Bash for loop
There are two ways you can define the bash for loop. The first is the bash style with two variables, and the second is C style with 3 variables. Hereâs the syntax for both of them.
**Bash Style for loop definition:**
```
for VARIABLE in SEQUENCE
```
The **VARIABLE** is the variable name to use within the loop, and the **SEQUENCE** is anything ranging from a sequence of numbers, multiple files, Linux commands, etc.
**C Style for loop definition:**
```
for (( variable declaration; condition; increment variable))
```
## How to Use the for Loop?
Now that you have a basic understanding of the **for loop**, itâs time to start using it in our scripts. Weâll start by creating a simple for loop that iterates over a given sequence of numbers.
```
#!/bin/bash
for i in 1 2 3 4 5 6 7
do
echo "Loop number $i"
done
```

For Loop In Shell Scripts
Very simple to understand? Letâs move on to a different sequence group.
### Bash For Loop Over Files
The for loop doesnât read the contents of the file when we loop over it but just allows us to perform operations on individual files while looping over them. Letâs see an example below.
```
#!/bin/bash
for i in *.txt
do
echo "Contents from the files: $i"
cat $i
done
```

For Loop Iterate Over Files
Iâve created two files with some text in them for demonstration purposes. See how I write the [cat command](https://www.linuxfordevices.com/tutorials/linux/cat-more-command-in-linux) once and the for loop iterates over both the files.
### Writing C Style for Loops in Shell Scripts
You should be comfortable with iterating over individual sequences now. Letâs move on to the C style for loops that we talked about before. If youâve already worked with C or C++, this is going to be almost the same thing. But even if you havenât itâs not too difficult.
Letâs create a for loop that iterates 7 times and echos the number.
```
for (( i=1; i<=7; i++ ))
do
echo "Loop number $i"
done
```

C Style For Loop
Letâs understand the loop section by section. The reason why we use the double parenthesis is to signal bash that weâre using a C style expression within the brackets.
- The first expression: **i=1** creates the start of the loop.
- The second expression: **i\<=7** tells that the loop should run until the variable is lesser than or equal to 7
- The last expression: **i++** decides how the increment happens. In this case, it happens by 1
## Conclusion
I hope this helps you understand how the for loop in shell scripts works. Feel free to comment below for any questions that you may have. |
| Shard | 36 (laksa) |
| Root Hash | 11864324050110837636 |
| Unparsed URL | com,linuxfordevices!www,/tutorials/shell-script/for-loop-in-shell-scripts s443 |