ℹ️ 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 | 2.4 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://medium.com/@codingcampus/bash-for-loop-with-examples-ff1d88b9ad42 |
| Last Crawled | 2026-02-08 16:57:04 (2 months ago) |
| First Indexed | not set |
| HTTP Status Code | 200 |
| Content | |
| Meta Title | Bash For Loop (With Examples). Bash for loop is a bash statement that… | by CodingCampus | Medium |
| Meta Description | Bash For Loop (With Examples) Bash for loop is a bash statement that allows code to be executed repeatedly. Loops can be used at the command line prompt or within a shell script. Bash for Loop Basic … |
| Meta Canonical | null |
| Boilerpipe Text | 3 min read
Nov 23, 2023
--
Press enter or click to view image in full size
Bash for loop is a bash statement that allows code to be executed repeatedly. Loops can be used at the command line prompt or within a shell script.
Bash for Loop Basic Syntax
The basic syntax of bash for loop is
for <variable> in <list>
do <commands>
done
<list>
is a sequence of any values (words, numbers, etc.) separated by spaces.
<commands>
is a set of any valid Bash command (usually executed over a list of items).
for
is a loop syntax keyword.
<variable>
is a bash variable name used to get access to the item from the list.
Here’s a basic bash for loop example.
for VARIABLE in "blue" "green" "red" "orange" "yellow"
do echo "$VARIABLE"
done
Press enter or click to view image in full size
A basic example of bash for loop.
You can see a list of our variables (blue, green, red, orange, yellow) displayed in the terminal.
Basic Bash For Loop Examples
Strings Example
In this example, we display the days of the work week.
for weekday in Monday Tuesday Wednesday Thursday Friday
do echo "Day of the week is $weekday"
done
Press enter or click to view image in full size
Strings example — bash for loop
Range Example
In this example, we count (the range) from
45
to
50
.
for num in {45..50}
do echo "The number is $num"
done
Press enter or click to view image in full size
Range example — bash for loop
Arrays Example
In this example, we iterate through the teams in the
nflteams
array.
nflteam=('Chiefs' 'Raiders' 'Broncos' 'Chargers')
for nflteam in "${nflteam[@]}"
do echo "NFL Team: $nflteam"
done
Press enter or click to view image in full size
Array example — bash for loop
Increments Example
In this example, we count from
0
to
40
in increments of
10
.
for num in {0..40..10}
do echo "The number is $num"
done
Press enter or click to view image in full size
Increments example — bash for loop
Infinite Loop Example
In this example, we create an infinite loop using an empty expression. The only way to stop the loop is by pressing
<Ctrl>C
.
for (( ; ; ))
do echo "This is an infinite loop! [Press CTRL+C to abort]"
done
Press enter or click to view image in full size
Infinite loop example -bash for loop
Break Statement Example
In this example, we cycle through the letters
K
through
Q
and break when we come to the letter
O
.
for letter in {K..Q}
do
if [[ "$letter" == 'O' ]]; then
break
fi
echo "The letter is: $letter"
done
Press enter or click to view image in full size
Break statement example — bash for loop
Continue Statement Example
In this example, we cycle through the numbers
1
through
10
. When we come to the number
5
, we skip the number and continue counting until we reach
10
.
for num in {1..10}
do
if [[ "$num" == '5' ]]; then
continue
fi
echo "The number is: $num"
done
Press enter or click to view image in full size
Continue statement example — bash for loop. |
| Markdown | [Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40codingcampus%2Fbash-for-loop-with-examples-ff1d88b9ad42&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[Medium Logo](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40codingcampus%2Fbash-for-loop-with-examples-ff1d88b9ad42&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

# [Bash For Loop (With Examples)](https://codingcampus.net/wp-admin/post.php?post=410&action=edit)
[](https://medium.com/@codingcampus?source=post_page---byline--ff1d88b9ad42---------------------------------------)
[CodingCampus](https://medium.com/@codingcampus?source=post_page---byline--ff1d88b9ad42---------------------------------------)
3 min read
·
Nov 23, 2023
\--
Listen
Share
Press enter or click to view image in full size
![]()
Bash for loop is a bash statement that allows code to be executed repeatedly. Loops can be used at the command line prompt or within a shell script.
## Bash for Loop Basic Syntax
The basic syntax of bash for loop is
```
for <variable> in <list>
do <commands>
done
```
- **\<list\>** is a sequence of any values (words, numbers, etc.) separated by spaces.
- **\<commands\>** is a set of any valid Bash command (usually executed over a list of items).
- **for** is a loop syntax keyword.
- **\<variable\>** is a bash variable name used to get access to the item from the list.
Here’s a basic bash for loop example.
```
for VARIABLE in "blue" "green" "red" "orange" "yellow"
do echo "$VARIABLE"
done
```
Press enter or click to view image in full size
![]()
A basic example of bash for loop.
You can see a list of our variables (blue, green, red, orange, yellow) displayed in the terminal.
## Basic Bash For Loop Examples
## Strings Example
In this example, we display the days of the work week.
```
for weekday in Monday Tuesday Wednesday Thursday Friday
do echo "Day of the week is $weekday"
done
```
Press enter or click to view image in full size
![]()
Strings example — bash for loop
## Range Example
In this example, we count (the range) from **45** to **50**.
```
for num in {45..50}
do echo "The number is $num"
done
```
Press enter or click to view image in full size
![]()
Range example — bash for loop
## Arrays Example
In this example, we iterate through the teams in the **nflteams** array.
```
nflteam=('Chiefs' 'Raiders' 'Broncos' 'Chargers')
for nflteam in "${nflteam[@]}"
do echo "NFL Team: $nflteam"
done
```
Press enter or click to view image in full size
![]()
Array example — bash for loop
## Increments Example
In this example, we count from **0** to **40** in increments of **10**.
```
for num in {0..40..10}
do echo "The number is $num"
done
```
Press enter or click to view image in full size
![]()
Increments example — bash for loop
## Infinite Loop Example
In this example, we create an infinite loop using an empty expression. The only way to stop the loop is by pressing **\<Ctrl\>C**.
```
for (( ; ; ))
do echo "This is an infinite loop! [Press CTRL+C to abort]"
done
```
Press enter or click to view image in full size
![]()
Infinite loop example -bash for loop
## Break Statement Example
In this example, we cycle through the letters **K** through **Q** and break when we come to the letter **O**.
```
for letter in {K..Q}
do
if [[ "$letter" == 'O' ]]; then
break
fi
echo "The letter is: $letter"
done
```
Press enter or click to view image in full size
![]()
Break statement example — bash for loop
## Continue Statement Example
In this example, we cycle through the numbers **1** through **10**. When we come to the number **5**, we skip the number and continue counting until we reach **10**.
```
for num in {1..10}
do
if [[ "$num" == '5' ]]; then
continue
fi
echo "The number is: $num"
done
```
Press enter or click to view image in full size
![]()
Continue statement example — bash for loop.
[Bash For Loop](https://medium.com/tag/bash-for-loop?source=post_page-----ff1d88b9ad42---------------------------------------)
[Unix](https://medium.com/tag/unix?source=post_page-----ff1d88b9ad42---------------------------------------)
[Linux](https://medium.com/tag/linux?source=post_page-----ff1d88b9ad42---------------------------------------)
[Coding](https://medium.com/tag/coding?source=post_page-----ff1d88b9ad42---------------------------------------)
\--
\--
[](https://medium.com/@codingcampus?source=post_page---post_author_info--ff1d88b9ad42---------------------------------------)
[](https://medium.com/@codingcampus?source=post_page---post_author_info--ff1d88b9ad42---------------------------------------)
[Written by CodingCampus](https://medium.com/@codingcampus?source=post_page---post_author_info--ff1d88b9ad42---------------------------------------)
[10 followers](https://medium.com/@codingcampus/followers?source=post_page---post_author_info--ff1d88b9ad42---------------------------------------)
·[1 following](https://medium.com/@codingcampus/following?source=post_page---post_author_info--ff1d88b9ad42---------------------------------------)
## No responses yet
[Help](https://help.medium.com/hc/en-us?source=post_page-----ff1d88b9ad42---------------------------------------)
[Status](https://status.medium.com/?source=post_page-----ff1d88b9ad42---------------------------------------)
[About](https://medium.com/about?autoplay=1&source=post_page-----ff1d88b9ad42---------------------------------------)
[Careers](https://medium.com/jobs-at-medium/work-at-medium-959d1a85284e?source=post_page-----ff1d88b9ad42---------------------------------------)
[Press](mailto:pressinquiries@medium.com)
[Blog](https://blog.medium.com/?source=post_page-----ff1d88b9ad42---------------------------------------)
[Privacy](https://policy.medium.com/medium-privacy-policy-f03bf92035c9?source=post_page-----ff1d88b9ad42---------------------------------------)
[Rules](https://policy.medium.com/medium-rules-30e5502c4eb4?source=post_page-----ff1d88b9ad42---------------------------------------)
[Terms](https://policy.medium.com/medium-terms-of-service-9db0094a1e0f?source=post_page-----ff1d88b9ad42---------------------------------------)
[Text to speech](https://speechify.com/medium?source=post_page-----ff1d88b9ad42---------------------------------------) |
| Readable Markdown | [](https://medium.com/@codingcampus?source=post_page---byline--ff1d88b9ad42---------------------------------------)
3 min read Nov 23, 2023
\--
Press enter or click to view image in full size
Bash for loop is a bash statement that allows code to be executed repeatedly. Loops can be used at the command line prompt or within a shell script.
## Bash for Loop Basic Syntax
The basic syntax of bash for loop is
```
for <variable> in <list>
do <commands>
done
```
- **\<list\>** is a sequence of any values (words, numbers, etc.) separated by spaces.
- **\<commands\>** is a set of any valid Bash command (usually executed over a list of items).
- **for** is a loop syntax keyword.
- **\<variable\>** is a bash variable name used to get access to the item from the list.
Here’s a basic bash for loop example.
```
for VARIABLE in "blue" "green" "red" "orange" "yellow"
do echo "$VARIABLE"
done
```
Press enter or click to view image in full size
A basic example of bash for loop.
You can see a list of our variables (blue, green, red, orange, yellow) displayed in the terminal.
## Basic Bash For Loop Examples
## Strings Example
In this example, we display the days of the work week.
```
for weekday in Monday Tuesday Wednesday Thursday Friday
do echo "Day of the week is $weekday"
done
```
Press enter or click to view image in full size
Strings example — bash for loop
## Range Example
In this example, we count (the range) from **45** to **50**.
```
for num in {45..50}
do echo "The number is $num"
done
```
Press enter or click to view image in full size
Range example — bash for loop
## Arrays Example
In this example, we iterate through the teams in the **nflteams** array.
```
nflteam=('Chiefs' 'Raiders' 'Broncos' 'Chargers')
for nflteam in "${nflteam[@]}"
do echo "NFL Team: $nflteam"
done
```
Press enter or click to view image in full size
Array example — bash for loop
## Increments Example
In this example, we count from **0** to **40** in increments of **10**.
```
for num in {0..40..10}
do echo "The number is $num"
done
```
Press enter or click to view image in full size
Increments example — bash for loop
## Infinite Loop Example
In this example, we create an infinite loop using an empty expression. The only way to stop the loop is by pressing **\<Ctrl\>C**.
```
for (( ; ; ))
do echo "This is an infinite loop! [Press CTRL+C to abort]"
done
```
Press enter or click to view image in full size
Infinite loop example -bash for loop
## Break Statement Example
In this example, we cycle through the letters **K** through **Q** and break when we come to the letter **O**.
```
for letter in {K..Q}
do
if [[ "$letter" == 'O' ]]; then
break
fi
echo "The letter is: $letter"
done
```
Press enter or click to view image in full size
Break statement example — bash for loop
## Continue Statement Example
In this example, we cycle through the numbers **1** through **10**. When we come to the number **5**, we skip the number and continue counting until we reach **10**.
```
for num in {1..10}
do
if [[ "$num" == '5' ]]; then
continue
fi
echo "The number is: $num"
done
```
Press enter or click to view image in full size
Continue statement example — bash for loop. |
| ML Classification | |
| ML Categories | null |
| ML Page Types | null |
| ML Intent Types | null |
| Content Metadata | |
| Language | en |
| Author | null |
| Publish Time | 2023-11-23 12:33:44 (2 years ago) |
| Original Publish Time | not set |
| Republished | Yes |
| Word Count (Total) | 615 |
| Word Count (Content) | 541 |
| Links | |
| External Links | 4 |
| Internal Links | 27 |
| Technical SEO | |
| Meta Nofollow | No |
| Meta Noarchive | Yes |
| JS Rendered | No |
| Redirect Target | null |
| Performance | |
| Download Time (ms) | 461 |
| TTFB (ms) | 460 |
| Download Size (bytes) | 30,398 |
| Shard | 77 (laksa) |
| Root Hash | 13179037029838926277 |
| Unparsed URL | com,medium!/@codingcampus/bash-for-loop-with-examples-ff1d88b9ad42 s443 |