ℹ️ 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://blog.eduonix.com/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/ |
| Last Crawled | 2026-04-07 07:04:52 (16 hours ago) |
| First Indexed | 2024-02-22 01:06:26 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Learn the concept of For Loop in Linux Shell Scripting - Eduonix Blog |
| Meta Description | In this article, we will talk about Loops. There are 3 types of loops supported in Linux Shells. Of the three types,we will learn about the for loop. |
| Meta Canonical | null |
| Boilerpipe Text | There will be cases wherein you need to repeatedly execute a block of code. Such repeated execution of portions of your code is done using what we call
Loops
. Linux Shell has three types of loops:
Â
The for loop
The while loop
The until loop
Each type of the three has its own syntax and unique usages. So, let’s start with the first type: the for loop.
Â
The
for
Loop
A very well-known loop type, supported in almost all programming languages.
Syntax:
for loop_var in list_of_values
do
statement1
statement1
…
statementn
done
where:
loop_var is the loop variable that loops (iterates) over all the values in the provided list.
list_of_values is a collection (or range) of all possible values that the loop variable could take.
statements1 through n constitute the loop body; this is the block of code to be executed repeatedly.
do and done are the delimiters that delimit the loop body.
Â
To get the idea closer to your mind, let’s illustrate it by example.
Â
Example
In this example, we are going to write a script that calculates the factorial of an integer using the for loop. From mathematics, we know that the factorial of an integer n is equal to:
Â
n * (n-1) * (n-2) …. * 2 * 1
Â
So, let’s see together how for loop could achieve this. Check the following code:
Should that work and calculate the factorial of the entered integer value?!
Yes, it worked as expected. Now, let’s explain the above code.
Line 1: The Shebang, you already know about it.
Lines 2-5: comments that document some info about the script.
Line 6: initializes the variable result to 1.
Line 7: uses the read command to prompt the user to enter a positive integer, and assigns the input to the variable x.
Line 8: starts a for loop with a loop variable i that iterates over the range of values that comes from the output of expansion of the command seq 1 x
Â
Lines 9 and 11: are the delimiters of the loop body.
Line 10: the only statement in the loop body. It uses the let command to make an accumulative multiplication using the *= operator. It multiplies the loop variable i by the variable result, and then stores the result in the variable result.
Line 12: prints the results back to the user.
* * * * * *
Â
In the previous example, we had a for loop that iterated over a sequence of numeric values. The for loop could also iterate over a list of: filenames, directories, words of a file, process IDs (PIDs). In the next example, we are going to use for loop to rename all the files in a directory.
Â
Example
Given a directory that contains a large number of files. Your manager has asked you to rename all the files in the directory by adding the suffix .backup to the end of each filename.
How would you do this?!
You have two choices: the first is to go ahead and start renaming every file, spending a whole day (and 8 cups of coffee), and leaving for home at last at 21:00. The other, is to think as a programmer, and start writing a short script that offloads this burden from your shoulders (and make your manager happy with you finishing this task in 5 minutes). Read the following script.
Let’s see how this short script will rename all the above files in one execution.
If you list the files in this directory again, you should find them renamed.
Now, to the explanation:
Line 6: defines a variable DIR, and assigns to it the full path of the directory containing the files to be renamed.
Line 7: starts the for loop. In this loop the loop variable f iterates over the list of files in the directory /root/testdir.
Lines 8 and 11: the do and done statements that act as loop body delimiters.
Lines 9 and 10: The loop body:
Line 9: prints a message that the script is renaming the file.
Line 10: uses the mv command to rename the file in hand.
So simple and logic, isn’t it?!
Â
* * * * * *
Â
Using for Loop to Kill Specific Type of Processes
Consider a case wherein you need to kill all processes started by a specific user, could for loop do something?! Sure. Let’s consider the following example.
Example
Your manager asked you to terminate any process started by the user postgres.
Again, you have two choices: either to list such processes using the ps command, and copy each PID and paste manually as an argument to the kill command, or… write the following script.
Now, let’s see it in action.
All processes have been killed in one script execution. Now, let’s explain things:
Line 5: defines the for loop whose variable pid iterates on the output from the command:
ps –o pid –u postgres | grep –v PID
which prints the PIDs (process IDs) of the processes owned by the user postgres.
Â
Lines 6 and 9: the do and done statements that mark the start and end of the loop body.
Lines 7 and 8: the loop body to be executed repeatedly for each PID.
Line 7: prints a message that the Process with the given PID will be killed.
Line 8: uses the kill command to terminate the process.
Isn’t it so beautiful?!
Â
* * * * * *
Â
In this article, we have started talking about Loops. There are 3 types of loops supported in Linux Shells. Of the three types, we have talked about the for loop. The for loop is very useful when we need to iterate over a range or a list of values. We have seen some situations wherein the for loop did a great job and saved time and effort. In the next article, we are going to talk on another great type:
the while loop
. So, see you there!!
Post navigation |
| Markdown | [Skip to content](https://blog.eduonix.com/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/#content)
April 7, 2026
- [Privacy Policy](https://blog.eduonix.com/privacy-policy/)
- [Terms & Conditions](https://blog.eduonix.com/terms-conditions/)
- [Write For Us](https://blog.eduonix.com/write-for-us/)
[Eduonix Blog](https://blog.eduonix.com/)
Learn and Build Your Skills
[](https://www.kickstarter.com/projects/eduonix/all-in-one-claude-ai-workflows-automation-and-more?ref=8ikwoq)
[Primary Menu](https://blog.eduonix.com/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/)
- [Privacy Policy](https://blog.eduonix.com/privacy-policy/)
- [Terms & Conditions](https://blog.eduonix.com/terms-conditions/)
- [Write For Us](https://blog.eduonix.com/write-for-us/)
- [Home](https://blog.eduonix.com/)
- [2015](https://blog.eduonix.com/2015/)
- [September](https://blog.eduonix.com/2015/09/)
- [Learn the concept of For Loop in Linux Shell Scripting](https://blog.eduonix.com/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/)
- [Shell Scripting](https://blog.eduonix.com/category/shell-scripting/)
# Learn the concept of For Loop in Linux Shell Scripting
[Tutor @ Eduonix](https://blog.eduonix.com/author/edututor/) September 4, 2015 5 minutes read

There will be cases wherein you need to repeatedly execute a block of code. Such repeated execution of portions of your code is done using what we call *Loops*. Linux Shell has three types of loops:
- The for loop
- The while loop
- The until loop
Each type of the three has its own syntax and unique usages. So, let’s start with the first type: the for loop.
**The****for** **Loop**
A very well-known loop type, supported in almost all programming languages.
**Syntax:**
```
for loop_var in list_of_values
do
statement1
statement1
…
statementn
done
```
**where:**
loop\_var is the loop variable that loops (iterates) over all the values in the provided list.
list\_of\_values is a collection (or range) of all possible values that the loop variable could take.
statements1 through n constitute the loop body; this is the block of code to be executed repeatedly.
do and done are the delimiters that delimit the loop body.
To get the idea closer to your mind, let’s illustrate it by example.
**Example**
In this example, we are going to write a script that calculates the factorial of an integer using the for loop. From mathematics, we know that the factorial of an integer n is equal to:
n \* (n-1) \* (n-2) …. \* 2 \* 1
So, let’s see together how for loop could achieve this. Check the following code:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/11.png)
Should that work and calculate the factorial of the entered integer value?\!
[](https://blog.eduonix.com/wp-content/uploads/2015/09/22.png)
Yes, it worked as expected. Now, let’s explain the above code.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/31.png)
- Line 1: The Shebang, you already know about it.
- Lines 2-5: comments that document some info about the script.
- Line 6: initializes the variable result to 1.
- Line 7: uses the read command to prompt the user to enter a positive integer, and assigns the input to the variable x.
- Line 8: starts a for loop with a loop variable i that iterates over the range of values that comes from the output of expansion of the command seq 1 x
[](https://blog.eduonix.com/wp-content/uploads/2015/09/41.png)
- Lines 9 and 11: are the delimiters of the loop body.
- Line 10: the only statement in the loop body. It uses the let command to make an accumulative multiplication using the \*= operator. It multiplies the loop variable i by the variable result, and then stores the result in the variable result.
- Line 12: prints the results back to the user.
\* \* \* \* \* \*
In the previous example, we had a for loop that iterated over a sequence of numeric values. The for loop could also iterate over a list of: filenames, directories, words of a file, process IDs (PIDs). In the next example, we are going to use for loop to rename all the files in a directory.
**Example**
Given a directory that contains a large number of files. Your manager has asked you to rename all the files in the directory by adding the suffix .backup to the end of each filename.
How would you do this?\!
[](https://blog.eduonix.com/wp-content/uploads/2015/09/51.png)
You have two choices: the first is to go ahead and start renaming every file, spending a whole day (and 8 cups of coffee), and leaving for home at last at 21:00. The other, is to think as a programmer, and start writing a short script that offloads this burden from your shoulders (and make your manager happy with you finishing this task in 5 minutes). Read the following script.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/61.png)
[](https://www.eduonix.com/courses/Software-Development/Learn-the-Basics-of-C-Programming-Language)
Let’s see how this short script will rename all the above files in one execution.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/71.png)
[](https://blog.eduonix.com/wp-content/uploads/2015/09/8.png)
[](https://blog.eduonix.com/wp-content/uploads/2015/09/9.png)
If you list the files in this directory again, you should find them renamed.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/10.png)
Now, to the explanation:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/111.png)
- Line 6: defines a variable DIR, and assigns to it the full path of the directory containing the files to be renamed.
- Line 7: starts the for loop. In this loop the loop variable f iterates over the list of files in the directory /root/testdir.
- Lines 8 and 11: the do and done statements that act as loop body delimiters.
- Lines 9 and 10: The loop body:
- Line 9: prints a message that the script is renaming the file.
- Line 10: uses the mv command to rename the file in hand.
So simple and logic, isn’t it?\!
\* \* \* \* \* \*
**Using for Loop to Kill Specific Type of Processes**
Consider a case wherein you need to kill all processes started by a specific user, could for loop do something?! Sure. Let’s consider the following example.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/12.png)
**Example**
Your manager asked you to terminate any process started by the user postgres.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/13.png)
Again, you have two choices: either to list such processes using the ps command, and copy each PID and paste manually as an argument to the kill command, or… write the following script.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/14.png)
Now, let’s see it in action.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/15.png)
All processes have been killed in one script execution. Now, let’s explain things:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/151.png)
- Line 5: defines the for loop whose variable pid iterates on the output from the command:
```
ps –o pid –u postgres | grep –v PID
```
which prints the PIDs (process IDs) of the processes owned by the user postgres.
- Lines 6 and 9: the do and done statements that mark the start and end of the loop body.
- Lines 7 and 8: the loop body to be executed repeatedly for each PID.
- Line 7: prints a message that the Process with the given PID will be killed.
- Line 8: uses the kill command to terminate the process.
Isn’t it so beautiful?\!
\* \* \* \* \* \*
In this article, we have started talking about Loops. There are 3 types of loops supported in Linux Shells. Of the three types, we have talked about the for loop. The for loop is very useful when we need to iterate over a range or a list of values. We have seen some situations wherein the for loop did a great job and saved time and effort. In the next article, we are going to talk on another great type: [the while loop](https://blog.eduonix.com/shell-scripting/learn-the-concept-of-while-loop-in-linux-shell-scripting/). So, see you there!\!
## About the Author

### [Tutor @ Eduonix](https://blog.eduonix.com/author/edututor/)
Administrator
[View All Posts](https://blog.eduonix.com/author/edututor/)
## Post navigation
[Previous: Learn Decision Making using if conditions in Linux Shell Scripting](https://blog.eduonix.com/2015/09/learn-decision-making-using-if-conditions-in-linux-shell-scripting/)
[Next: Everything You Need to Know About Bitmaps for Android Apps](https://blog.eduonix.com/2015/09/everything-you-need-to-know-about-bitmaps-for-android-apps/)
### Leave a Reply [Cancel reply](https://blog.eduonix.com/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/#respond)
## Related Articles

- [Shell Scripting](https://blog.eduonix.com/category/shell-scripting/)
### [Generating Random Numbers in Linux Shell Scripting](https://blog.eduonix.com/2020/02/generating-random-numbers-in-linux-shell-scripting/)
[Tutor @ Eduonix](https://blog.eduonix.com/author/edututor/) February 27, 2020

- [Shell Scripting](https://blog.eduonix.com/category/shell-scripting/)
### [Learn CGI scripting using BASH in Linux Shell Scripting](https://blog.eduonix.com/2016/01/learn-cgi-scripting-using-bash-in-linux-shell-scripting/)
[Tutor @ Eduonix](https://blog.eduonix.com/author/edututor/) January 11, 2016

- [Shell Scripting](https://blog.eduonix.com/category/shell-scripting/)
### [Learn about Netcat: your network Swiss knife in Shell Scripting](https://blog.eduonix.com/2016/01/learn-about-netcat-your-network-swiss-knife-in-shell-scripting/)
[Tutor @ Eduonix](https://blog.eduonix.com/author/edututor/) January 2, 2016
Copyright © Eduonix Learning Solutions. All rights reserved. \| [MoreNews](https://afthemes.com/products/morenews/) by AF themes. |
| Readable Markdown | There will be cases wherein you need to repeatedly execute a block of code. Such repeated execution of portions of your code is done using what we call *Loops*. Linux Shell has three types of loops:
- The for loop
- The while loop
- The until loop
Each type of the three has its own syntax and unique usages. So, let’s start with the first type: the for loop.
**The**
**for** **Loop**
A very well-known loop type, supported in almost all programming languages.
**Syntax:**
```
for loop_var in list_of_values
do
statement1
statement1
…
statementn
done
```
**where:**
loop\_var is the loop variable that loops (iterates) over all the values in the provided list.
list\_of\_values is a collection (or range) of all possible values that the loop variable could take.
statements1 through n constitute the loop body; this is the block of code to be executed repeatedly.
do and done are the delimiters that delimit the loop body.
To get the idea closer to your mind, let’s illustrate it by example.
**Example**
In this example, we are going to write a script that calculates the factorial of an integer using the for loop. From mathematics, we know that the factorial of an integer n is equal to:
n \* (n-1) \* (n-2) …. \* 2 \* 1
So, let’s see together how for loop could achieve this. Check the following code:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/11.png)
Should that work and calculate the factorial of the entered integer value?\!
[](https://blog.eduonix.com/wp-content/uploads/2015/09/22.png)
Yes, it worked as expected. Now, let’s explain the above code.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/31.png)
- Line 1: The Shebang, you already know about it.
- Lines 2-5: comments that document some info about the script.
- Line 6: initializes the variable result to 1.
- Line 7: uses the read command to prompt the user to enter a positive integer, and assigns the input to the variable x.
- Line 8: starts a for loop with a loop variable i that iterates over the range of values that comes from the output of expansion of the command seq 1 x
[](https://blog.eduonix.com/wp-content/uploads/2015/09/41.png)
- Lines 9 and 11: are the delimiters of the loop body.
- Line 10: the only statement in the loop body. It uses the let command to make an accumulative multiplication using the \*= operator. It multiplies the loop variable i by the variable result, and then stores the result in the variable result.
- Line 12: prints the results back to the user.
\* \* \* \* \* \*
In the previous example, we had a for loop that iterated over a sequence of numeric values. The for loop could also iterate over a list of: filenames, directories, words of a file, process IDs (PIDs). In the next example, we are going to use for loop to rename all the files in a directory.
**Example**
Given a directory that contains a large number of files. Your manager has asked you to rename all the files in the directory by adding the suffix .backup to the end of each filename.
How would you do this?\!
[](https://blog.eduonix.com/wp-content/uploads/2015/09/51.png)
You have two choices: the first is to go ahead and start renaming every file, spending a whole day (and 8 cups of coffee), and leaving for home at last at 21:00. The other, is to think as a programmer, and start writing a short script that offloads this burden from your shoulders (and make your manager happy with you finishing this task in 5 minutes). Read the following script.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/61.png)
[](https://www.eduonix.com/courses/Software-Development/Learn-the-Basics-of-C-Programming-Language)
Let’s see how this short script will rename all the above files in one execution.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/71.png)
[](https://blog.eduonix.com/wp-content/uploads/2015/09/8.png)
[](https://blog.eduonix.com/wp-content/uploads/2015/09/9.png)
If you list the files in this directory again, you should find them renamed.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/10.png)
Now, to the explanation:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/111.png)
- Line 6: defines a variable DIR, and assigns to it the full path of the directory containing the files to be renamed.
- Line 7: starts the for loop. In this loop the loop variable f iterates over the list of files in the directory /root/testdir.
- Lines 8 and 11: the do and done statements that act as loop body delimiters.
- Lines 9 and 10: The loop body:
- Line 9: prints a message that the script is renaming the file.
- Line 10: uses the mv command to rename the file in hand.
So simple and logic, isn’t it?\!
\* \* \* \* \* \*
**Using for Loop to Kill Specific Type of Processes**
Consider a case wherein you need to kill all processes started by a specific user, could for loop do something?! Sure. Let’s consider the following example.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/12.png)
**Example**
Your manager asked you to terminate any process started by the user postgres.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/13.png)
Again, you have two choices: either to list such processes using the ps command, and copy each PID and paste manually as an argument to the kill command, or… write the following script.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/14.png)
Now, let’s see it in action.
[](https://blog.eduonix.com/wp-content/uploads/2015/09/15.png)
All processes have been killed in one script execution. Now, let’s explain things:
[](https://blog.eduonix.com/wp-content/uploads/2015/09/151.png)
- Line 5: defines the for loop whose variable pid iterates on the output from the command:
```
ps –o pid –u postgres | grep –v PID
```
which prints the PIDs (process IDs) of the processes owned by the user postgres.
- Lines 6 and 9: the do and done statements that mark the start and end of the loop body.
- Lines 7 and 8: the loop body to be executed repeatedly for each PID.
- Line 7: prints a message that the Process with the given PID will be killed.
- Line 8: uses the kill command to terminate the process.
Isn’t it so beautiful?\!
\* \* \* \* \* \*
In this article, we have started talking about Loops. There are 3 types of loops supported in Linux Shells. Of the three types, we have talked about the for loop. The for loop is very useful when we need to iterate over a range or a list of values. We have seen some situations wherein the for loop did a great job and saved time and effort. In the next article, we are going to talk on another great type: [the while loop](https://blog.eduonix.com/shell-scripting/learn-the-concept-of-while-loop-in-linux-shell-scripting/). So, see you there!\!

## Post navigation |
| Shard | 130 (laksa) |
| Root Hash | 967453079524984130 |
| Unparsed URL | com,eduonix!blog,/2015/09/learn-the-concept-of-for-loop-in-linux-shell-scripting/ s443 |