ℹ️ 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.tecmint.com/bash-for-loop-linux/ |
| Last Crawled | 2026-04-11 01:32:47 (15 hours ago) |
| First Indexed | 2022-12-09 07:47:04 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Use Bash For Loop in Linux: A Beginner's Tutorial |
| Meta Description | In programming languages, Loops are essential components and are used when you want to repeat code over and over again until a specified condition is met. |
| Meta Canonical | null |
| Boilerpipe Text | In programming languages,
Loops
are essential components and are used when you want to repeat code over and over again until a specified condition is met.
In
Bash
scripting,
loops
play much the same role and are used to
automate repetitive tasks
just like in programming languages.
In
Bash
scripting, there are 3 types of
loops
:
for loop
,
while loop
, and
until loop
. The three are used to iterate over a list of values and perform a given set of commands.
In this guide, we will focus on the
Bash For Loop
in Linux.
Table of Contents
Bash For Loop Syntax
Bash For Loop Example
Bash For Loop with Ranges
Bash For Loops with Arrays
Bash C Style For Loop
Bash C-styled For Loops With Conditional Statements
Use the ‘Continue’ statement with Bash For Loop
Use the ‘break’ statement with Bash For Loop
Conclusion
Bash For Loop Syntax
As mentioned earlier, the
for loop
iterates over a range of values and executes a
set of Linux commands
.
For loop
takes the following syntax:
for variable_name in value1 value2 value3 .. n
do
command1
command2
commandn
done
Let us now check a few example usages of the bash for loop.
Bash For Loop Example
In its simplest form, the
for loop
takes the following basic format. In this example, the variable
n
iterates over a group of numerical values enclosed in curly braces and prints out their values to stdout.
for n in {1 2 3 4 5 6 7};
do
echo $n
done
Bash For Loop Example
Bash For Loop with Ranges
In the previous examples, we explicitly listed the values to be iterated by the
for loop
, which works just fine. However, you can only imagine how cumbersome and time-consuming a task it would be if you were to iterate over, for example, a hundred values. This would compel you to type all the values from 1 to 100.
To address this issue, specify a range. To do so, specify the number to start and stop separated by two periods.
In this example, 1 is the first value whilst 7 is the last value in the range.
#!/bin/bash
for n in {1..7};
do
echo $n
done
Once the shell script is executed, all the values in the range are listed, similar to what we had in
simple loops
.
Bash For Loop with Ranges Example
Additionally, we can include a value at the end of the range that is going to cause the
for loop
to iterate through the values in incremental steps.
The following bash script prints the values between 1 and 7 with 2 incremental steps between the values starting from the first value.
#!/bin/bash
for n in {1..7..2};
do
echo $n
done
Bash For Loop Incremented Values
From the above example, you can see that the
loop
incremented the values inside the curly braces by 2 values.
Bash For Loops with Arrays
You can also easily iterate through values defined in an array using a
for Loop
. In the following example, the
for loop
iterates through all the values inside the
fruits array
and prints them to stdout.
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[@]};
do
echo $n
done
Bash For Loop Array Example
The
@
operator accesses or targets all the elements. This makes it possible to iterate over all the elements one by one.
In addition, you can access a single element by specifying its position within the array.
For example to access the “
mango
” element, replace the
@
operator with the position of the element in the array (the first element starts at 0, so in this case, “
mango
” will be denoted by 2).
This is what the for loop looks like.
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[2]};
do
echo $n
done
Bash For Loops with Array Elements
Bash C Style For Loop
You can use variables inside loops to iterate over a range of elements. This is where
C-styled for loops
come in. The following example illustrates a
C-style for loop
that prints out a list of numerical values from 1 to 7.
#!/bin/bash
n=7
for (( n=1 ; n<=$n ; n++ ));
do
echo $n
done
Bash C-styled For Loops Example
Bash C-styled For Loops With Conditional Statements
You can include conditional statements inside
C-styled for loops
. In the following example, we have included an if-else statement that checks and prints out even and odd numbers between 1 and 7.
#!/bin/bash
for (( n=1; n<=7; n++ ))
do
# Check if the number is even or not
if (( $n%2==0 ))
then
echo "$n is even"
else
echo "$n is odd"
fi
done
Bash C-styled For Loops Conditional Statements Example
Use the ‘Continue’ statement with Bash For Loop
The ‘
continue
‘ statement is a built-in command that controls how a script runs. Apart from bash scripting, it is also used in programming languages such as
Python
and Java.
The
continue statement
halts the current iteration inside a
loop
when a specific condition is met, and then resumes the iteration.
Consider the
for loop
shown below.
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
continue
fi
echo $n
done
Bash For Loop Continue Statement Example
This is what the code does:
Line 2
: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
Line 4
: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and restarts the loop at the next iteration in line 2.
Line 9
: Prints the values to the screen only if the condition in line 4 is false.
The following is the expected output after running the script.
Bash For Loop Continue Statement Output
Use the ‘break’ statement with Bash For Loop
The
‘break’
statement, as the name suggests, halts or ends the iteration when a condition is met.
Consider the
For loop
below.
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
break
fi
echo $n
done
echo "All done"
Bash For Loop Break Statement
This is what the code does:
Line 2
: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
Line 4
: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and halts the iteration.
Line 9
: Prints the numbers to the screen only if the condition in line 4 is false.
From the output, you can see that the loop stops once the variable meets the condition of the loop.
Bash For Loop Break Statement Output
Conclusion
That was a tutorial about
Bash For loops
. We hope you found this insightful. Feel free to weigh in with your feedback. |
| Markdown | [Skip to content](https://www.tecmint.com/bash-for-loop-linux/#content "Skip to content")
[](https://www.tecmint.com/ "Tecmint: Linux Howtos, Tutorials & Guides")
Menu
Menu
- [Learn Linux](https://www.tecmint.com/free-online-linux-learning-guide-for-beginners/ "Start Learning Linux")
- [Linux Distros](https://www.tecmint.com/best-linux-distributions/ "Linux Distributions")
- [Linux Distros for Beginners](https://www.tecmint.com/best-linux-distributions-for-beginners/)
- [Linux Distros for Experts](https://www.tecmint.com/linux-distro-for-power-users/ "Widely Used Distros")
- [New Linux Distros](https://www.tecmint.com/new-linux-distributions/)
- [Linux Server Distros](https://www.tecmint.com/10-best-linux-server-distributions/)
- [Secure Linux Distros](https://www.tecmint.com/best-security-centric-linux-distributions/)
- [CentOS Alternatives](https://www.tecmint.com/centos-alternative-distributions/ "CentOS Alternative Distros")
- [RedHat Distributions](https://www.tecmint.com/redhat-based-linux-distributions/ "RedHat Based Distros")
- [Debian Distributions](https://www.tecmint.com/debian-based-linux-distributions/ "Debian Based Distros")
- [Ubuntu Distributions](https://www.tecmint.com/ubuntu-based-linux-distributions/ "Ubuntu Based Distros")
- [Arch Linux Distros](https://www.tecmint.com/arch-based-linux-distributions/ "Arch Linux Based Distros")
- [Rolling Linux Distros](https://www.tecmint.com/best-rolling-release-linux-distributions/)
- [KDE Linux Distros](https://www.tecmint.com/best-linux-distributions-for-kde-plasma/ "KDE Based Distros")
- [Linux Distros for Old PC](https://www.tecmint.com/linux-distributions-for-old-computers/ "Linux Distros for Older Computers")
- [Linux Distros for Kids](https://www.tecmint.com/best-linux-distributions-for-kids/)
- [Linux Distributions for Students](https://www.tecmint.com/linux-distros-students/)
- [Linux Distros for Windows](https://www.tecmint.com/best-alternative-linux-distributions-for-windows-users/)
- [Commands](https://www.tecmint.com/category/linux-commands/ "Linux Commands")
- [A – Z Linux Commands](https://www.tecmint.com/linux-commands-cheat-sheet/)
- [100+ Linux Commands](https://www.tecmint.com/essential-linux-commands/ "Essential Linux Commands")
- [Tools](https://www.tecmint.com/category/top-tools/ "Best Linux Software")
- [Pro Courses](https://www.tecmint.com/bash-for-loop-linux/ "Linux Online Courses")
- [Bash Scripting](https://pro.tecmint.com/learn-bash-scripting/ "Bash Scripting for Beginners")
- [Learn Linux](https://pro.tecmint.com/learn-linux/ "Master Linux in 7 Days")
- [AI for Linux](https://pro.tecmint.com/ai-for-linux/ "AI for Linux Course")
- [RHCSA Certification](https://pro.tecmint.com/rhcsa-certification-course/ "RHCSA Certification Course")
- [RHCE Certification](https://pro.tecmint.com/rhce-certification-course/ "RHCE Certification Course")
- [LFCS Certification](https://pro.tecmint.com/lfcs-certification-course/ "LFCS Certification Course")
- [eBooks](https://tecmint.gumroad.com/ "Download eBooks")
- [RHCSA Exam](https://www.tecmint.com/rhcsa-exam-reviewing-essential-commands-system-documentation/ "RHCSA Certification eBook")
- [RHCE Exam](https://www.tecmint.com/how-to-setup-and-configure-static-network-routing-in-rhel/ "RHCE Certification eBook")
- [LFCS Exam](https://www.tecmint.com/sed-command-to-create-edit-and-manipulate-files-in-linux/ "LFCS Certification eBook")
- [LFCE Exam](https://www.tecmint.com/installing-network-services-and-configuring-services-at-system-boot/ "LFCE Certification eBook")
- [LFCA Exam](https://www.tecmint.com/understanding-linux-operating-system/ "LFCA Certification eBook")
- [Ansible Exam](https://www.tecmint.com/understand-core-components-of-ansible/ "Ansible Certification eBook")
- [Sign In](https://pro.tecmint.com/#/portal/account)
- [Join Root →](https://pro.tecmint.com/#/portal)
[](https://www.tecmint.com/ "Tecmint: Linux Howtos, Tutorials & Guides")
Menu
- [Learn Linux](https://www.tecmint.com/free-online-linux-learning-guide-for-beginners/ "Start Learning Linux")
- [Linux Distros](https://www.tecmint.com/best-linux-distributions/ "Linux Distributions")
- [Linux Distros for Beginners](https://www.tecmint.com/best-linux-distributions-for-beginners/)
- [Linux Distros for Experts](https://www.tecmint.com/linux-distro-for-power-users/ "Widely Used Distros")
- [New Linux Distros](https://www.tecmint.com/new-linux-distributions/)
- [Linux Server Distros](https://www.tecmint.com/10-best-linux-server-distributions/)
- [Secure Linux Distros](https://www.tecmint.com/best-security-centric-linux-distributions/)
- [CentOS Alternatives](https://www.tecmint.com/centos-alternative-distributions/ "CentOS Alternative Distros")
- [RedHat Distributions](https://www.tecmint.com/redhat-based-linux-distributions/ "RedHat Based Distros")
- [Debian Distributions](https://www.tecmint.com/debian-based-linux-distributions/ "Debian Based Distros")
- [Ubuntu Distributions](https://www.tecmint.com/ubuntu-based-linux-distributions/ "Ubuntu Based Distros")
- [Arch Linux Distros](https://www.tecmint.com/arch-based-linux-distributions/ "Arch Linux Based Distros")
- [Rolling Linux Distros](https://www.tecmint.com/best-rolling-release-linux-distributions/)
- [KDE Linux Distros](https://www.tecmint.com/best-linux-distributions-for-kde-plasma/ "KDE Based Distros")
- [Linux Distros for Old PC](https://www.tecmint.com/linux-distributions-for-old-computers/ "Linux Distros for Older Computers")
- [Linux Distros for Kids](https://www.tecmint.com/best-linux-distributions-for-kids/)
- [Linux Distributions for Students](https://www.tecmint.com/linux-distros-students/)
- [Linux Distros for Windows](https://www.tecmint.com/best-alternative-linux-distributions-for-windows-users/)
- [Commands](https://www.tecmint.com/category/linux-commands/ "Linux Commands")
- [A – Z Linux Commands](https://www.tecmint.com/linux-commands-cheat-sheet/)
- [100+ Linux Commands](https://www.tecmint.com/essential-linux-commands/ "Essential Linux Commands")
- [Tools](https://www.tecmint.com/category/top-tools/ "Best Linux Software")
- [Pro Courses](https://www.tecmint.com/bash-for-loop-linux/ "Linux Online Courses")
- [Bash Scripting](https://pro.tecmint.com/learn-bash-scripting/ "Bash Scripting for Beginners")
- [Learn Linux](https://pro.tecmint.com/learn-linux/ "Master Linux in 7 Days")
- [AI for Linux](https://pro.tecmint.com/ai-for-linux/ "AI for Linux Course")
- [RHCSA Certification](https://pro.tecmint.com/rhcsa-certification-course/ "RHCSA Certification Course")
- [RHCE Certification](https://pro.tecmint.com/rhce-certification-course/ "RHCE Certification Course")
- [LFCS Certification](https://pro.tecmint.com/lfcs-certification-course/ "LFCS Certification Course")
- [eBooks](https://tecmint.gumroad.com/ "Download eBooks")
- [RHCSA Exam](https://www.tecmint.com/rhcsa-exam-reviewing-essential-commands-system-documentation/ "RHCSA Certification eBook")
- [RHCE Exam](https://www.tecmint.com/how-to-setup-and-configure-static-network-routing-in-rhel/ "RHCE Certification eBook")
- [LFCS Exam](https://www.tecmint.com/sed-command-to-create-edit-and-manipulate-files-in-linux/ "LFCS Certification eBook")
- [LFCE Exam](https://www.tecmint.com/installing-network-services-and-configuring-services-at-system-boot/ "LFCE Certification eBook")
- [LFCA Exam](https://www.tecmint.com/understanding-linux-operating-system/ "LFCA Certification eBook")
- [Ansible Exam](https://www.tecmint.com/understand-core-components-of-ansible/ "Ansible Certification eBook")
- [Sign In](https://pro.tecmint.com/#/portal/account)
- [Join Root →](https://pro.tecmint.com/#/portal)
# How to Use Bash For Loop with Examples in Linux
[James Kiarie](https://www.tecmint.com/author/james2030kiarie/ "View all posts by James Kiarie")
Last Updated: June 6, 2023
Read Time: 4 mins
Categories [Bash Shell](https://www.tecmint.com/category/bash-shell/), [Shell Scripting](https://www.tecmint.com/category/shell-scripting/)
[1 Comment](https://www.tecmint.com/bash-for-loop-linux/#comments)
Take Your Linux Skills to the Next Level All courses, certifications, ad-free articles & community — from \$8/mo
[Join Root →](https://pro.tecmint.com/#/portal/signup)
See what's included *▾*
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
From **\$8/mo** · or **\$59/yr** billed annually · Cancel anytime
In programming languages, **Loops** are essential components and are used when you want to repeat code over and over again until a specified condition is met.
In **Bash** scripting, **loops** play much the same role and are used to [automate repetitive tasks](https://www.tecmint.com/using-shell-script-to-automate-linux-system-maintenance-tasks/ "Using Shell Scripting to Automate Linux System Tasks") just like in programming languages.
In **Bash** scripting, there are 3 types of **loops**: **for loop**, **while loop**, and **until loop**. The three are used to iterate over a list of values and perform a given set of commands.
In this guide, we will focus on the **Bash For Loop** in Linux.
Table of Contents
Toggle
- [Bash For Loop Syntax](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_Syntax)
- [Bash For Loop Example](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_Example)
- [Bash For Loop with Ranges](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_with_Ranges)
- [Bash For Loops with Arrays](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loops_with_Arrays)
- [Bash C Style For Loop](https://www.tecmint.com/bash-for-loop-linux/#Bash_C_Style_For_Loop)
- [Bash C-styled For Loops With Conditional Statements](https://www.tecmint.com/bash-for-loop-linux/#Bash_C-styled_For_Loops_With_Conditional_Statements)
- [Use the ‘Continue’ statement with Bash For Loop](https://www.tecmint.com/bash-for-loop-linux/#Use_the_%E2%80%98Continue_statement_with_Bash_For_Loop)
- [Use the ‘break’ statement with Bash For Loop](https://www.tecmint.com/bash-for-loop-linux/#Use_the_%E2%80%98break_statement_with_Bash_For_Loop)
- - - [Conclusion](https://www.tecmint.com/bash-for-loop-linux/#Conclusion)
## Bash For Loop Syntax
As mentioned earlier, the **for loop** iterates over a range of values and executes a [set of Linux commands](https://www.tecmint.com/essential-linux-commands/ "Essential Linux Commands").
**For loop** takes the following syntax:
```
for variable_name in value1 value2 value3 .. n
do
command1
command2
commandn
done
```
Let us now check a few example usages of the bash for loop.
## Bash For Loop Example
In its simplest form, the **for loop** takes the following basic format. In this example, the variable `n` iterates over a group of numerical values enclosed in curly braces and prints out their values to stdout.
```
for n in {1 2 3 4 5 6 7};
do
echo $n
done
```

Bash For Loop Example
## Bash For Loop with Ranges
In the previous examples, we explicitly listed the values to be iterated by the **for loop**, which works just fine. However, you can only imagine how cumbersome and time-consuming a task it would be if you were to iterate over, for example, a hundred values. This would compel you to type all the values from 1 to 100.
To address this issue, specify a range. To do so, specify the number to start and stop separated by two periods.
In this example, 1 is the first value whilst 7 is the last value in the range.
```
#!/bin/bash
for n in {1..7};
do
echo $n
done
```
Once the shell script is executed, all the values in the range are listed, similar to what we had in **simple loops**.

Bash For Loop with Ranges Example
Additionally, we can include a value at the end of the range that is going to cause the **for loop** to iterate through the values in incremental steps.
The following bash script prints the values between 1 and 7 with 2 incremental steps between the values starting from the first value.
```
#!/bin/bash
for n in {1..7..2};
do
echo $n
done
```

Bash For Loop Incremented Values
From the above example, you can see that the **loop** incremented the values inside the curly braces by 2 values.
## Bash For Loops with Arrays
You can also easily iterate through values defined in an array using a **for Loop**. In the following example, the **for loop** iterates through all the values inside the **fruits array** and prints them to stdout.
```
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[@]};
do
echo $n
done
```

Bash For Loop Array Example
The `@` operator accesses or targets all the elements. This makes it possible to iterate over all the elements one by one.
In addition, you can access a single element by specifying its position within the array.
For example to access the “**mango**” element, replace the `@` operator with the position of the element in the array (the first element starts at 0, so in this case, “**mango**” will be denoted by 2).
This is what the for loop looks like.
```
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[2]};
do
echo $n
done
```

Bash For Loops with Array Elements
## Bash C Style For Loop
You can use variables inside loops to iterate over a range of elements. This is where **C-styled for loops** come in. The following example illustrates a **C-style for loop** that prints out a list of numerical values from 1 to 7.
```
#!/bin/bash
n=7
for (( n=1 ; n<=$n ; n++ ));
do
echo $n
done
```

Bash C-styled For Loops Example
## Bash C-styled For Loops With Conditional Statements
You can include conditional statements inside **C-styled for loops**. In the following example, we have included an if-else statement that checks and prints out even and odd numbers between 1 and 7.
```
#!/bin/bash
for (( n=1; n<=7; n++ ))
do
# Check if the number is even or not
if (( $n%2==0 ))
then
echo "$n is even"
else
echo "$n is odd"
fi
done
```

Bash C-styled For Loops Conditional Statements Example
## Use the ‘Continue’ statement with Bash For Loop
The ‘**continue**‘ statement is a built-in command that controls how a script runs. Apart from bash scripting, it is also used in programming languages such as [Python](https://www.tecmint.com/install-python-in-linux/ "Install Python from Source in Linux") and Java.
The **continue statement** halts the current iteration inside a **loop** when a specific condition is met, and then resumes the iteration.
Consider the **for loop** shown below.
```
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
continue
fi
echo $n
done
```

Bash For Loop Continue Statement Example
This is what the code does:
- **Line 2**: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
- **Line 4**: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and restarts the loop at the next iteration in line 2.
- **Line 9**: Prints the values to the screen only if the condition in line 4 is false.
The following is the expected output after running the script.

Bash For Loop Continue Statement Output
## Use the ‘break’ statement with Bash For Loop
The **‘break’** statement, as the name suggests, halts or ends the iteration when a condition is met.
Consider the **For loop** below.
```
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
break
fi
echo $n
done
echo "All done"
```

Bash For Loop Break Statement
This is what the code does:
- **Line 2**: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
- **Line 4**: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and halts the iteration.
- **Line 9**: Prints the numbers to the screen only if the condition in line 4 is false.
From the output, you can see that the loop stops once the variable meets the condition of the loop.

Bash For Loop Break Statement Output
##### Conclusion
That was a tutorial about **Bash For loops**. We hope you found this insightful. Feel free to weigh in with your feedback.
Pro TecMint Root Plan
Premium Linux Education for Serious Learners
### Take Your Linux Skills to the Next Level
Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.
What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
[Join Root Plan →](https://pro.tecmint.com/#/portal/signup) **\$8/mo** · or **\$59/yr** billed annually Cancel anytime
Previous article:
[How to Fix “bash syntax error near unexpected token” in Linux](https://www.tecmint.com/bash-syntax-error-near-unexpected-token/)
Next article:
[13 Practical Examples of Using the Gzip Command in Linux](https://www.tecmint.com/gzip-command-in-linux/)

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.
***
*Each tutorial at **TecMint** is created by a team of experienced Linux system administrators* so that it meets our high-quality *standards.*
Join the **[TecMint Weekly Newsletter](https://newsletter.tecmint.com/subscription?f=hj6Ohm9gck3Z0PQ2BBBTh892iaCbDV3jJJa3hD8ULUlubOgnbo8aF44vt2HZfdc36g)** (More Than **156,129** Linux Enthusiasts Have Subscribed)
**Was this article helpful?** Please **[add a comment](https://www.tecmint.com/bash-for-loop-linux/#respond)** or **[buy me a coffee](https://www.buymeacoffee.com/tecmint)** to show your appreciation.
## Related Posts
[](https://www.tecmint.com/bash-vs-zsh-vs-fish/ "Terminal Showdown: Bash vs Zsh vs Fish for Power Users")
[Terminal Showdown: Bash vs Zsh vs Fish for Power Users](https://www.tecmint.com/bash-vs-zsh-vs-fish/)
[](https://www.tecmint.com/mathematical-operations-in-shell-scripting/ "Calculating Mathematical Expressions in Shell Scripting – Part 5")
[Calculating Mathematical Expressions in Shell Scripting – Part 5](https://www.tecmint.com/mathematical-operations-in-shell-scripting/)
[](https://www.tecmint.com/rbash-restricted-bash-shell/ "rbash – A Restricted Bash Shell Explained with Practical Examples")
[rbash – A Restricted Bash Shell Explained with Practical Examples](https://www.tecmint.com/rbash-restricted-bash-shell/)
[](https://www.tecmint.com/basic-mathematical-operations-in-bash/ "Learn Basic Mathematical Operations in Bash Scripting – Part IV")
[Learn Basic Mathematical Operations in Bash Scripting – Part IV](https://www.tecmint.com/basic-mathematical-operations-in-bash/)
[](https://www.tecmint.com/sailing-through-the-world-of-linux-bash-scripting-part-iii/ "Learn Practical BASH Scripting Projects – Part III")
[Learn Practical BASH Scripting Projects – Part III](https://www.tecmint.com/sailing-through-the-world-of-linux-bash-scripting-part-iii/)
[](https://www.tecmint.com/basic-shell-programming-part-ii/ "5 Useful Shell Scripts for Linux Newbies – Part II")
[5 Useful Shell Scripts for Linux Newbies – Part II](https://www.tecmint.com/basic-shell-programming-part-ii/)
## 1 Comment
[Leave a Reply](https://www.tecmint.com/bash-for-loop-linux/#reply-title)
1. 
Ian Williams
[December 10, 2022 at 10:45 pm](https://www.tecmint.com/bash-for-loop-linux/#comment-1927676)
Please test your examples. The very first example you give does not do what you say it does. I print the curly brackets, which is not what you show.
I’m going back to c++. At least that does what it says on the tin\!
[Reply](https://www.tecmint.com/bash-for-loop-linux/#comment-1927676)
### Got Something to Say? Join the Discussion... [Cancel reply](https://www.tecmint.com/bash-for-loop-linux/#respond)
Pro TecMint · Root Plan
### Take Your Linux Skills to the Next Level
Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.
What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
[Join Root Plan →](https://pro.tecmint.com/#/portal/signup)
**\$8/mo** · or **\$59/yr** Cancel anytime
Pro TecMint Root Plan
14 courses
### Premium Linux Courses
[Learn Linux in 7 Days A complete 7-day crash course to help beginners master Linux, the command line, and software management. No experience needed.](https://pro.tecmint.com/learn-linux/?utm_source=tecmint&utm_medium=sidebar&utm_campaign=learn-linux)
[Linux Interview Handbook A complete 3-part interview preparation series with 240+ questions covering Linux basics to advanced DevOps tools.](https://pro.tecmint.com/linux-interview-questions/?utm_source=tecmint&utm_medium=sidebar&utm_campaign=interview)
[100+ Essential Linux Commands Build real Linux skills with 100+ commands and practical examples from Ubuntu, Fedora, and Debian.](https://pro.tecmint.com/linux-commands-series/?utm_source=tecmint&utm_medium=sidebar&utm_campaign=linux-commands)
[Red Hat Certified System Administrator (RHCSA) Learn RHEL 10 from scratch and confidently pass the EX200 certification exam.](https://pro.tecmint.com/rhcsa-certification-course/?utm_source=tecmint&utm_medium=sidebar&utm_campaign=rhcsa)
[11 Best Linux Distributions Explore the best Linux distributions, learn how to install them safely, and get comfortable using their main features.](https://pro.tecmint.com/best-linux-distributions/?utm_source=tecmint&utm_medium=sidebar&utm_campaign=linux-distros)
[Join Root — Access All Courses →](https://pro.tecmint.com/#/portal/signup?utm_source=tecmint&utm_medium=sidebar&utm_campaign=root)
From **\$8/mo** · or **\$59/yr** · Cancel anytime
## Linux Server Monitoring Tools
[BpyTop – Resource Monitoring Tool for Linux](https://www.tecmint.com/bpytop-linux-resource-monitor-tool/)
[How to Install Zabbix Monitoring Tool on Debian 11/10](https://www.tecmint.com/install-zabbix-on-debian-10/)
[How to Install dbWatch to Monitor MySQL Performance in Linux](https://www.tecmint.com/dbwatch-monitor-mysql-performance/)
[Nmon – Monitor Linux System and Network Performance](https://www.tecmint.com/nmon-linux-performance-monitor/)
[How to Install LibreNMS Monitoring Tool on Debian 11/10](https://www.tecmint.com/install-librenms-debian/)
[TCPflow – Analyze and Debug Network Traffic in Linux](https://www.tecmint.com/tcpflow-analyze-debug-network-traffic-in-linux/)
## Learn Linux Tricks & Tips
[An Easy Way to Hide Files and Directories in Linux](https://www.tecmint.com/hide-files-and-directories-in-linux/)
[How to Find Recent or Today’s Modified Files in Linux](https://www.tecmint.com/find-recent-modified-files-in-linux/)
[How to Create a Password Protected ZIP File in Linux](https://www.tecmint.com/create-password-protected-zip-file-in-linux/)
[How to Find Linux Server Geographic Location in Terminal](https://www.tecmint.com/find-linux-server-geographic-location/)
[How to Christmassify Your Linux Terminal and Shell](https://www.tecmint.com/christmassify-linux-terminal-and-shell/)
[Find Top Running Processes by Highest Memory and CPU Usage in Linux](https://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/)
## Best Linux Tools
[5 Best Open Source Internet Radio Player for Linux](https://www.tecmint.com/internet-radio-player-linux/)
[8 Best Open-Source Disk Cloning & Backup Tools for Linux (2024)](https://www.tecmint.com/linux-disk-cloning-tools/)
[6 Best Command-Line FTP Clients for Linux Users](https://www.tecmint.com/command-line-ftp-clients-for-linux/)
[10 Best Linux File and Disk Encryption Tools (2024)](https://www.tecmint.com/file-and-disk-encryption-tools-for-linux/)
[25 Outstanding Backup Utilities for Linux Systems in 2024](https://www.tecmint.com/linux-system-backup-tools/)
[6 Best Whiteboard Applications for Your Linux Systems](https://www.tecmint.com/linux-whiteboard-applications/)
Privacy Manager
Tecmint: Linux Howtos, Tutorials & Guides © 2026. All Rights Reserved.
The material in this site cannot be republished either online or offline, without our permission.
Hosting Sponsored by : [Linode Cloud Hosting](https://www.tecmint.com/go/linode)
✕
Pro TecMint Root Plan Premium Linux Education for Serious Learners
### Before You Go - Upgrade Your Linux Skills
Root members get everything in one place, with new courses added every month.
What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Linux certifications: RHCSA, RHCE, LFCS and LFCA
Access new courses on release
Weekly newsletter, priority support & Telegram community
Join Root Today and Start Learning Linux the Right Way
Structured courses, certification prep, and a community of Linux professionals - all in one membership.
[Join Root Plan →](https://pro.tecmint.com/#/portal/signup)
**\$8/mo** · or **\$59/yr** billed annually
No thanks, I'll continue reading |
| Readable Markdown | In programming languages, **Loops** are essential components and are used when you want to repeat code over and over again until a specified condition is met.
In **Bash** scripting, **loops** play much the same role and are used to [automate repetitive tasks](https://www.tecmint.com/using-shell-script-to-automate-linux-system-maintenance-tasks/ "Using Shell Scripting to Automate Linux System Tasks") just like in programming languages.
In **Bash** scripting, there are 3 types of **loops**: **for loop**, **while loop**, and **until loop**. The three are used to iterate over a list of values and perform a given set of commands.
In this guide, we will focus on the **Bash For Loop** in Linux.
Table of Contents
- [Bash For Loop Syntax](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_Syntax)
- [Bash For Loop Example](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_Example)
- [Bash For Loop with Ranges](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loop_with_Ranges)
- [Bash For Loops with Arrays](https://www.tecmint.com/bash-for-loop-linux/#Bash_For_Loops_with_Arrays)
- [Bash C Style For Loop](https://www.tecmint.com/bash-for-loop-linux/#Bash_C_Style_For_Loop)
- [Bash C-styled For Loops With Conditional Statements](https://www.tecmint.com/bash-for-loop-linux/#Bash_C-styled_For_Loops_With_Conditional_Statements)
- [Use the ‘Continue’ statement with Bash For Loop](https://www.tecmint.com/bash-for-loop-linux/#Use_the_%E2%80%98Continue_statement_with_Bash_For_Loop)
- [Use the ‘break’ statement with Bash For Loop](https://www.tecmint.com/bash-for-loop-linux/#Use_the_%E2%80%98break_statement_with_Bash_For_Loop)
- - - [Conclusion](https://www.tecmint.com/bash-for-loop-linux/#Conclusion)
## Bash For Loop Syntax
As mentioned earlier, the **for loop** iterates over a range of values and executes a [set of Linux commands](https://www.tecmint.com/essential-linux-commands/ "Essential Linux Commands").
**For loop** takes the following syntax:
```
for variable_name in value1 value2 value3 .. n
do
command1
command2
commandn
done
```
Let us now check a few example usages of the bash for loop.
## Bash For Loop Example
In its simplest form, the **for loop** takes the following basic format. In this example, the variable `n` iterates over a group of numerical values enclosed in curly braces and prints out their values to stdout.
```
for n in {1 2 3 4 5 6 7};
do
echo $n
done
```

Bash For Loop Example
## Bash For Loop with Ranges
In the previous examples, we explicitly listed the values to be iterated by the **for loop**, which works just fine. However, you can only imagine how cumbersome and time-consuming a task it would be if you were to iterate over, for example, a hundred values. This would compel you to type all the values from 1 to 100.
To address this issue, specify a range. To do so, specify the number to start and stop separated by two periods.
In this example, 1 is the first value whilst 7 is the last value in the range.
```
#!/bin/bash
for n in {1..7};
do
echo $n
done
```
Once the shell script is executed, all the values in the range are listed, similar to what we had in **simple loops**.

Bash For Loop with Ranges Example
Additionally, we can include a value at the end of the range that is going to cause the **for loop** to iterate through the values in incremental steps.
The following bash script prints the values between 1 and 7 with 2 incremental steps between the values starting from the first value.
```
#!/bin/bash
for n in {1..7..2};
do
echo $n
done
```

Bash For Loop Incremented Values
From the above example, you can see that the **loop** incremented the values inside the curly braces by 2 values.
## Bash For Loops with Arrays
You can also easily iterate through values defined in an array using a **for Loop**. In the following example, the **for loop** iterates through all the values inside the **fruits array** and prints them to stdout.
```
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[@]};
do
echo $n
done
```

Bash For Loop Array Example
The `@` operator accesses or targets all the elements. This makes it possible to iterate over all the elements one by one.
In addition, you can access a single element by specifying its position within the array.
For example to access the “**mango**” element, replace the `@` operator with the position of the element in the array (the first element starts at 0, so in this case, “**mango**” will be denoted by 2).
This is what the for loop looks like.
```
#!/bin/bash
fruits=("blueberry" "peach" "mango" "pineapple" "papaya")
for n in ${fruits[2]};
do
echo $n
done
```

Bash For Loops with Array Elements
## Bash C Style For Loop
You can use variables inside loops to iterate over a range of elements. This is where **C-styled for loops** come in. The following example illustrates a **C-style for loop** that prints out a list of numerical values from 1 to 7.
```
#!/bin/bash
n=7
for (( n=1 ; n<=$n ; n++ ));
do
echo $n
done
```

Bash C-styled For Loops Example
## Bash C-styled For Loops With Conditional Statements
You can include conditional statements inside **C-styled for loops**. In the following example, we have included an if-else statement that checks and prints out even and odd numbers between 1 and 7.
```
#!/bin/bash
for (( n=1; n<=7; n++ ))
do
# Check if the number is even or not
if (( $n%2==0 ))
then
echo "$n is even"
else
echo "$n is odd"
fi
done
```

Bash C-styled For Loops Conditional Statements Example
## Use the ‘Continue’ statement with Bash For Loop
The ‘**continue**‘ statement is a built-in command that controls how a script runs. Apart from bash scripting, it is also used in programming languages such as [Python](https://www.tecmint.com/install-python-in-linux/ "Install Python from Source in Linux") and Java.
The **continue statement** halts the current iteration inside a **loop** when a specific condition is met, and then resumes the iteration.
Consider the **for loop** shown below.
```
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
continue
fi
echo $n
done
```

Bash For Loop Continue Statement Example
This is what the code does:
- **Line 2**: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
- **Line 4**: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and restarts the loop at the next iteration in line 2.
- **Line 9**: Prints the values to the screen only if the condition in line 4 is false.
The following is the expected output after running the script.

Bash For Loop Continue Statement Output
## Use the ‘break’ statement with Bash For Loop
The **‘break’** statement, as the name suggests, halts or ends the iteration when a condition is met.
Consider the **For loop** below.
```
#!/bin/bash
for n in {1..10}
do
if [[ $n -eq '6' ]]
then
echo "Target $n has been reached"
break
fi
echo $n
done
echo "All done"
```

Bash For Loop Break Statement
This is what the code does:
- **Line 2**: Marks the beginning of the for loop and iterate the variable n from 1 to 10.
- **Line 4**: Checks the value of n and if the variable is equal to 6, the script echoes a message to stdout and halts the iteration.
- **Line 9**: Prints the numbers to the screen only if the condition in line 4 is false.
From the output, you can see that the loop stops once the variable meets the condition of the loop.

Bash For Loop Break Statement Output
##### Conclusion
That was a tutorial about **Bash For loops**. We hope you found this insightful. Feel free to weigh in with your feedback. |
| Shard | 69 (laksa) |
| Root Hash | 17998126746619863669 |
| Unparsed URL | com,tecmint!www,/bash-for-loop-linux/ s443 |