ℹ️ 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 | 1.3 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://stackoverflow.com/questions/17872159/for-loop-in-unix |
| Last Crawled | 2026-03-04 09:09:06 (1 month ago) |
| First Indexed | not set |
| HTTP Status Code | 200 |
| Meta Title | shell - For loop in Unix - Stack Overflow |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | 1
I am new to Unix programming and I am not able to figure out what is wrong with this code:
#!/bin/sh
i=1
max=10
for i in {1..$max}
do
echo $i;
done
If I try the above code as follows it works:
#!/bin/sh
i=1
max=10
for i in {1..10}
do
echo $i;
done
I have tried this:
#!/bin/sh
i=1
max=10
for i in {1..`echo $max`}
do
echo $i;
done
and
#!/bin/sh
i=1
max=10
for i in {1..`expr $max`}
do
echo $i;
done
and
#!/bin/sh
i=1
max=10
for i in {1..`echo $max | bc`}
do
echo $i;
done
But it is also not working.. Can anyone tell me how come it will work..?
asked
Jul 26, 2013 at 2:59
0
3
Bash/zsh support much more faster and flexible form:
for ((i=1; i<$max; ++i));
Don't use external commands (like seq), or backticks - it will slow down your script.
answered
Jul 26, 2013 at 3:58
Comments
2
Maybe you can try this
#!/bin/sh
max=10
for i in $(seq 1 $max)
do
echo "$i"
done
You can see this
answer
brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.
Update:
In the case of you want a sequence of custom increment, the man page of
seq
gives the following:
seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
Therefore you can use
seq 1 3 $max
to get a sequence with increment 3.
In general,
#!/bin/sh
max=10
incr=3
for i in $(seq 1 $incr $max)
do
echo "$i"
done
answered
Jul 26, 2013 at 3:08
1 Comment
This is slow in comparison to built-in command. See my answer.
2013-08-06T18:44:40.373Z+00:00
1
Sequence expressions of the form
{x..y}
only take place when
x
and
y
are literal numbers or single characters. It takes place before variable expansion. If the limits can include variables, use the
seq
command:
for i in $(seq 1 $max)
answered
Jul 26, 2013 at 3:04
Barmar
790k
58 gold badges
555 silver badges
670 bronze badges
Comments
1
The bash man page says this:
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly tex-
tual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
So it appears that bash just passes through verbatim whatever text is in the braces. I wasn't familiar with that syntax, and I had to look it up. It doesn't sound like it was intended to be used in for loops.
bash has newer ways of doing this, but the traditional way is
for i in $(seq 1 $max)
do
# whatever...
done
Since pretty much anything can be done in bash with enough effort, and I couldn't turn down the challenge, here's how you could do it with braces anyway:
for i in $(eval echo {1..$max})
do
echo $i
done
answered
Jul 26, 2013 at 3:20
1 Comment
That
eval
is not needed.
2013-07-29T08:41:26.29Z+00:00
Start asking to get answers
Find the answer to your question by asking.
Ask question
Explore related questions
See similar questions with these tags. |
| Markdown | [Skip to main content](https://stackoverflow.com/questions/17872159/for-loop-in-unix#content)
1. [About](https://stackoverflow.co/)
2. Products
3. [For Teams](https://stackoverflow.co/internal/)
4. Try new site Try BETA
1. [Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools.](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-for-teams)
2. [Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content.](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-api)
3. [Stack Ads Connect your brand to the world’s most trusted technologist communities.](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-advertising)
4. [Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal.](https://stackoverflow.blog/releases/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=releases)
5. [About the company](https://stackoverflow.co/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=about-the-company) [Visit the blog](https://stackoverflow.blog/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=blog)
1. ### [current community](https://stackoverflow.com/)
- [Stack Overflow](https://stackoverflow.com/)
[help](https://stackoverflow.com/help) [chat](https://chat.stackoverflow.com/?tab=explore)
- [Meta Stack Overflow](https://meta.stackoverflow.com/)
### your communities
[Sign up](https://stackoverflow.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F17872159%2Ffor-loop-in-unix) or [log in](https://stackoverflow.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F17872159%2Ffor-loop-in-unix) to customize your list.
### [more stack exchange communities](https://stackexchange.com/sites)
[company blog](https://stackoverflow.blog/)
2. [Log in](https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F17872159%2Ffor-loop-in-unix)
3. [Sign up](https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F17872159%2Ffor-loop-in-unix)
1. 1. [Home](https://stackoverflow.com/)
2. [Questions](https://stackoverflow.com/questions)
3. [AI Assist](https://stackoverflow.com/ai-assist)
4. [Tags](https://stackoverflow.com/tags)
5. [Challenges](https://stackoverflow.com/beta/challenges)
6. [Chat](https://chat.stackoverflow.com/?tab=explore)
7. [Articles](https://stackoverflow.blog/contributed?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=so-blog&utm_content=experiment-articles)
8. [Users](https://stackoverflow.com/users)
9. [Companies](https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav)
10. [Collectives]()
11. Communities for your favorite technologies. [Explore all Collectives](https://stackoverflow.com/collectives-all)
2. Stack Internal
Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work.
[Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams)
3. [Stack Internal]()
4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact)
##### Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
[Learn more about Collectives](https://stackoverflow.com/collectives)
**Stack Internal**
Knowledge at work
Bring the best of human thought and AI automation together at your work.
[Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover)
# [For loop in Unix](https://stackoverflow.com/questions/17872159/for-loop-in-unix)
[Ask Question](https://stackoverflow.com/questions/ask)
Asked
12 years, 7 months ago
Modified [12 years, 7 months ago](https://stackoverflow.com/questions/17872159/for-loop-in-unix?lastactivity "2013-07-29 08:26:53Z")
Viewed 16k times
1
I am new to Unix programming and I am not able to figure out what is wrong with this code:
```
#!/bin/sh
i=1
max=10
for i in {1..$max}
do
echo $i;
done
```
If I try the above code as follows it works:
```
#!/bin/sh
i=1
max=10
for i in {1..10}
do
echo $i;
done
```
I have tried this:
```
#!/bin/sh
i=1
max=10
for i in {1..`echo $max`}
do
echo $i;
done
```
and
```
#!/bin/sh
i=1
max=10
for i in {1..`expr $max`}
do
echo $i;
done
```
and
```
#!/bin/sh
i=1
max=10
for i in {1..`echo $max | bc`}
do
echo $i;
done
```
But it is also not working.. Can anyone tell me how come it will work..?
- [shell](https://stackoverflow.com/questions/tagged/shell "show questions tagged 'shell'")
- [unix](https://stackoverflow.com/questions/tagged/unix "show questions tagged 'unix'")
[Share](https://stackoverflow.com/q/17872159 "Short permalink to this question")
[Improve this question](https://stackoverflow.com/posts/17872159/edit)
Follow
asked Jul 26, 2013 at 2:59
[](https://stackoverflow.com/users/1393856/veer-shrivastav)
[Veer Shrivastav](https://stackoverflow.com/users/1393856/veer-shrivastav)
5,4961111 gold badges5858 silver badges8484 bronze badges
0
[Add a comment](https://stackoverflow.com/questions/17872159/for-loop-in-unix "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.") \|
## 4 Answers 4
Sorted by:
[Reset to default](https://stackoverflow.com/questions/17872159/for-loop-in-unix?answertab=scoredesc#tab-top)
3
Bash/zsh support much more faster and flexible form:
```
for ((i=1; i<$max; ++i));
```
Don't use external commands (like seq), or backticks - it will slow down your script.
[Share](https://stackoverflow.com/a/17872663 "Short permalink to this answer")
[Improve this answer](https://stackoverflow.com/posts/17872663/edit)
Follow
answered Jul 26, 2013 at 3:58
[](https://stackoverflow.com/users/1173542/leonid-volnitsky)
[Leonid Volnitsky](https://stackoverflow.com/users/1173542/leonid-volnitsky)
9,25455 gold badges4242 silver badges5656 bronze badges
Sign up to request clarification or add additional context in comments.
## Comments
Add a comment
2
Maybe you can try this
```
#!/bin/sh
max=10
for i in $(seq 1 $max)
do
echo "$i"
done
```
You can see this [answer](https://stackoverflow.com/a/1445507/1371471)
> brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.
Update:
In the case of you want a sequence of custom increment, the man page of `seq` gives the following:
```
seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
```
Therefore you can use `seq 1 3 $max` to get a sequence with increment 3.
In general,
```
#!/bin/sh
max=10
incr=3
for i in $(seq 1 $incr $max)
do
echo "$i"
done
```
[Share](https://stackoverflow.com/a/17872236 "Short permalink to this answer")
[Improve this answer](https://stackoverflow.com/posts/17872236/edit)
Follow
[edited May 23, 2017 at 12:29](https://stackoverflow.com/posts/17872236/revisions "show all edits to this post")
[](https://stackoverflow.com/users/-1/community)
[Community](https://stackoverflow.com/users/-1/community)Bot
111 silver badge
answered Jul 26, 2013 at 3:08
[](https://stackoverflow.com/users/1371471/lazywei)
[lazywei](https://stackoverflow.com/users/1371471/lazywei)
12\.6k66 gold badges2323 silver badges2626 bronze badges
## 1 Comment
Add a comment
[](https://stackoverflow.com/users/1173542/leonid-volnitsky)
Leonid Volnitsky
[Leonid Volnitsky](https://stackoverflow.com/users/1173542/leonid-volnitsky)
[Over a year ago](https://stackoverflow.com/questions/17872159/for-loop-in-unix#comment26475448_17872236)
This is slow in comparison to built-in command. See my answer.
2013-08-06T18:44:40.373Z+00:00
0
Reply
- Copy link
1
Sequence expressions of the form `{x..y}` only take place when `x` and `y` are literal numbers or single characters. It takes place before variable expansion. If the limits can include variables, use the `seq` command:
```
for i in $(seq 1 $max)
```
[Share](https://stackoverflow.com/a/17872203 "Short permalink to this answer")
[Improve this answer](https://stackoverflow.com/posts/17872203/edit)
Follow
answered Jul 26, 2013 at 3:04
[](https://stackoverflow.com/users/1491895/barmar)
[Barmar](https://stackoverflow.com/users/1491895/barmar)
790k5858 gold badges555555 silver badges670670 bronze badges
## Comments
Add a comment
1
The bash man page says this:
> Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly tex- tual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
So it appears that bash just passes through verbatim whatever text is in the braces. I wasn't familiar with that syntax, and I had to look it up. It doesn't sound like it was intended to be used in for loops.
bash has newer ways of doing this, but the traditional way is
```
for i in $(seq 1 $max)
do
# whatever...
done
```
Since pretty much anything can be done in bash with enough effort, and I couldn't turn down the challenge, here's how you could do it with braces anyway:
```
for i in $(eval echo {1..$max})
do
echo $i
done
```
[Share](https://stackoverflow.com/a/17872357 "Short permalink to this answer")
[Improve this answer](https://stackoverflow.com/posts/17872357/edit)
Follow
answered Jul 26, 2013 at 3:20
[](https://stackoverflow.com/users/1951747/user1951747)
[user1951747](https://stackoverflow.com/users/1951747/user1951747)
7133 bronze badges
## 1 Comment
Add a comment
[](https://stackoverflow.com/users/612462/adrian-fr%C3%BChwirth)
Adrian FrĂĽhwirth
[Adrian FrĂĽhwirth](https://stackoverflow.com/users/612462/adrian-fr%C3%BChwirth)
[Over a year ago](https://stackoverflow.com/questions/17872159/for-loop-in-unix#comment26178803_17872357)
That `eval` is not needed.
2013-07-29T08:41:26.29Z+00:00
0
Reply
- Copy link
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://stackoverflow.com/questions/ask)
Explore related questions
- [shell](https://stackoverflow.com/questions/tagged/shell "show questions tagged 'shell'")
- [unix](https://stackoverflow.com/questions/tagged/unix "show questions tagged 'unix'")
See similar questions with these tags.
- The Overflow Blog
- [No need for Ctrl+C when you have MCP](https://stackoverflow.blog/2026/03/03/no-need-for-ctrl-c-when-you-have-mcp/)
- [AI-assisted coding needs more than vibes; it needs containers and sandboxes](https://stackoverflow.blog/2026/03/04/ai-assisted-coding-vibes-hardened-containers-and-sandboxes/)
- Featured on Meta
- [Logo updates to Stack Overflow's visual identity](https://meta.stackexchange.com/questions/417394/logo-updates-to-stack-overflows-visual-identity)
- [Policy: Generative AI (e.g., ChatGPT) is banned](https://meta.stackoverflow.com/questions/421831/policy-generative-ai-e-g-chatgpt-is-banned)
- [New site design and philosophy for Stack Overflow: Starting February 24, 2026...](https://meta.stackoverflow.com/questions/438177/new-site-design-and-philosophy-for-stack-overflow-starting-february-24-2026-at "New site design and philosophy for Stack Overflow: Starting February 24, 2026 at beta.stackoverflow.com")
- [I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s...](https://meta.stackoverflow.com/questions/438369/i-m-jody-the-chief-product-and-technology-officer-at-stack-overflow-let-s-talk "I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s talk about the site redesign")
#### Linked
[280](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax "Question score (upvotes - downvotes)")
[Shell script "for" loop syntax](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax?noredirect=1)
#### Related
[280](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax "Question score (upvotes - downvotes)")
[Shell script "for" loop syntax](https://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax)
[1](https://stackoverflow.com/questions/1894786/problem-with-for-loop-in-shell "Question score (upvotes - downvotes)")
[Problem with for loop in shell](https://stackoverflow.com/questions/1894786/problem-with-for-loop-in-shell)
[0](https://stackoverflow.com/questions/7198765/for-loop-in-shell "Question score (upvotes - downvotes)")
[for loop in shell](https://stackoverflow.com/questions/7198765/for-loop-in-shell)
[0](https://stackoverflow.com/questions/23727072/how-do-i-use-for-loop "Question score (upvotes - downvotes)")
[How do I use for loop?](https://stackoverflow.com/questions/23727072/how-do-i-use-for-loop)
[1](https://stackoverflow.com/questions/27628016/for-loop-in-unix-shell-scripting "Question score (upvotes - downvotes)")
[For loop in unix shell scripting](https://stackoverflow.com/questions/27628016/for-loop-in-unix-shell-scripting)
[1](https://stackoverflow.com/questions/31089036/for-loop-in-shell-script "Question score (upvotes - downvotes)")
[For loop in shell script](https://stackoverflow.com/questions/31089036/for-loop-in-shell-script)
[0](https://stackoverflow.com/questions/48573939/for-loop-at-the-bash-commandline "Question score (upvotes - downvotes)")
[for loop at the bash commandline](https://stackoverflow.com/questions/48573939/for-loop-at-the-bash-commandline)
[5](https://stackoverflow.com/questions/48595763/for-loop-in-linux-terminal "Question score (upvotes - downvotes)")
[For Loop in Linux Terminal](https://stackoverflow.com/questions/48595763/for-loop-in-linux-terminal)
[0](https://stackoverflow.com/questions/64700165/bash-for-loop-in-command-line "Question score (upvotes - downvotes)")
[bash - for loop in command line](https://stackoverflow.com/questions/64700165/bash-for-loop-in-command-line)
[1](https://stackoverflow.com/questions/74312126/for-loop-in-shell-scripting "Question score (upvotes - downvotes)")
[For Loop in Shell Scripting](https://stackoverflow.com/questions/74312126/for-loop-in-shell-scripting)
#### [Hot Network Questions](https://stackexchange.com/questions?tab=hot)
- [I cannot remove the through axle on my Trek Madone](https://bicycles.stackexchange.com/questions/100057/i-cannot-remove-the-through-axle-on-my-trek-madone)
- [Tenuto AND a tie?](https://music.stackexchange.com/questions/143321/tenuto-and-a-tie)
- [For which strongly connected digraphs can we reverse all edges one by one while maintaining strong connectivity?](https://cstheory.stackexchange.com/questions/57015/for-which-strongly-connected-digraphs-can-we-reverse-all-edges-one-by-one-while)
- [How are teaching portfolios evaluated when returning to academia after several years in industry?](https://academia.stackexchange.com/questions/226074/how-are-teaching-portfolios-evaluated-when-returning-to-academia-after-several-y)
- [Are there any other contour solutions to this integral?](https://math.stackexchange.com/questions/5127123/are-there-any-other-contour-solutions-to-this-integral)
- [A “lateral thinking” puzzle](https://puzzling.stackexchange.com/questions/137266/a-lateral-thinking-puzzle)
- [Would narrow cylinders be more efficient in piston engines because the gas won't expand in useless direction?](https://mechanics.stackexchange.com/questions/101978/would-narrow-cylinders-be-more-efficient-in-piston-engines-because-the-gas-wont)
- [How to identify a process occasionally calling a suspicious IP address?](https://serverfault.com/questions/1198342/how-to-identify-a-process-occasionally-calling-a-suspicious-ip-address)
- [How much will Voyager 1’s trajectory diverge from a straight-line extrapolation over 100–1000 years?](https://astronomy.stackexchange.com/questions/62233/how-much-will-voyager-1-s-trajectory-diverge-from-a-straight-line-extrapolation)
- [Can last\_commit\_lsn and last\_redone\_lsn in sys.dm\_hadr\_database\_replica\_states differ on a secondary replica?](https://dba.stackexchange.com/questions/349890/can-last-commit-lsn-and-last-redone-lsn-in-sys-dm-hadr-database-replica-states-d)
- [Visiting the US after overstaying as a minor](https://travel.stackexchange.com/questions/203359/visiting-the-us-after-overstaying-as-a-minor)
- [Power ORing with MOSFETs](https://electronics.stackexchange.com/questions/766503/power-oring-with-mosfets)
- [How do Ring of mind shielding and the soul cage spell interact?](https://rpg.stackexchange.com/questions/218882/how-do-ring-of-mind-shielding-and-the-soul-cage-spell-interact)
- [If the redo queue is always processed in transaction log order, how does it benefit from being done in parallel?](https://dba.stackexchange.com/questions/349889/if-the-redo-queue-is-always-processed-in-transaction-log-order-how-does-it-bene)
- [Is there any biblical or theological basis for believing that Adam and Eve themselves observed a Sabbath prior to the Fall?](https://christianity.stackexchange.com/questions/113269/is-there-any-biblical-or-theological-basis-for-believing-that-adam-and-eve-thems)
- [Decadent time travellers - prank involving handshakes versus kisses](https://scifi.stackexchange.com/questions/303493/decadent-time-travellers-prank-involving-handshakes-versus-kisses)
- [A shortage of floppies happened at the end of 80's or the beginning of 90's, for a few months... For what reason?](https://retrocomputing.stackexchange.com/questions/32508/a-shortage-of-floppies-happened-at-the-end-of-80s-or-the-beginning-of-90s-for)
- [Book where a mob mobilizes after a child predator, but is targeting the wrong person](https://literature.stackexchange.com/questions/31737/book-where-a-mob-mobilizes-after-a-child-predator-but-is-targeting-the-wrong-pe)
- [How to label axes, points, and curves in a 3D plot using luadraw?](https://tex.stackexchange.com/questions/760391/how-to-label-axes-points-and-curves-in-a-3d-plot-using-luadraw)
- [Where/how does gravity enter thermodynamics?](https://physics.stackexchange.com/questions/869740/where-how-does-gravity-enter-thermodynamics)
- [Simple wireshark dumpcap clone - packet capture wrapper for pcap](https://codereview.stackexchange.com/questions/301484/simple-wireshark-dumpcap-clone-packet-capture-wrapper-for-pcap)
- [What is the risk of water penetration in my rusting tub?](https://diy.stackexchange.com/questions/329457/what-is-the-risk-of-water-penetration-in-my-rusting-tub)
- [How is the number of seats of each land (state) in Bundestag determined?](https://politics.stackexchange.com/questions/94316/how-is-the-number-of-seats-of-each-land-state-in-bundestag-determined)
- [Race through the city of circular roads](https://puzzling.stackexchange.com/questions/137283/race-through-the-city-of-circular-roads)
[more hot questions](https://stackoverflow.com/questions/17872159/for-loop-in-unix)
[Question feed](https://stackoverflow.com/feeds/question/17872159 "Feed of this question and its answers")
# Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-bash
##### [Stack Overflow](https://stackoverflow.com/)
- [Questions](https://stackoverflow.com/questions)
- [Help](https://stackoverflow.com/help)
- [Chat](https://chat.stackoverflow.com/?tab=explore)
##### [Business](https://stackoverflow.co/)
- [Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams)
- [Stack Data Licensing](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=data-licensing)
- [Stack Ads](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising)
##### [Company](https://stackoverflow.co/)
- [About](https://stackoverflow.co/)
- [Press](https://stackoverflow.co/company/press/)
- [Work Here](https://stackoverflow.co/company/work-here/)
- [Legal](https://stackoverflow.com/legal)
- [Privacy Policy](https://stackoverflow.com/legal/privacy-policy)
- [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public)
- [Contact Us](https://stackoverflow.com/contact)
- Cookie Settings
- [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy)
##### [Stack Exchange Network](https://stackexchange.com/)
- [Technology](https://stackexchange.com/sites#technology)
- [Culture & recreation](https://stackexchange.com/sites#culturerecreation)
- [Life & arts](https://stackexchange.com/sites#lifearts)
- [Science](https://stackexchange.com/sites#science)
- [Professional](https://stackexchange.com/sites#professional)
- [Business](https://stackexchange.com/sites#business)
- [API](https://api.stackexchange.com/)
- [Data](https://data.stackexchange.com/)
- [Blog](https://stackoverflow.blog/?blb=1)
- [Facebook](https://www.facebook.com/officialstackoverflow/)
- [Twitter](https://twitter.com/stackoverflow)
- [LinkedIn](https://linkedin.com/company/stack-overflow)
- [Instagram](https://www.instagram.com/thestackoverflow)
Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.3.3.40393 |
| Readable Markdown | 1
I am new to Unix programming and I am not able to figure out what is wrong with this code:
```
#!/bin/sh
i=1
max=10
for i in {1..$max}
do
echo $i;
done
```
If I try the above code as follows it works:
```
#!/bin/sh
i=1
max=10
for i in {1..10}
do
echo $i;
done
```
I have tried this:
```
#!/bin/sh
i=1
max=10
for i in {1..`echo $max`}
do
echo $i;
done
```
and
```
#!/bin/sh
i=1
max=10
for i in {1..`expr $max`}
do
echo $i;
done
```
and
```
#!/bin/sh
i=1
max=10
for i in {1..`echo $max | bc`}
do
echo $i;
done
```
But it is also not working.. Can anyone tell me how come it will work..?
asked Jul 26, 2013 at 2:59
[](https://stackoverflow.com/users/1393856/veer-shrivastav)
0
3
Bash/zsh support much more faster and flexible form:
```
for ((i=1; i<$max; ++i));
```
Don't use external commands (like seq), or backticks - it will slow down your script.
answered Jul 26, 2013 at 3:58
[](https://stackoverflow.com/users/1173542/leonid-volnitsky)
Comments
2
Maybe you can try this
```
#!/bin/sh
max=10
for i in $(seq 1 $max)
do
echo "$i"
done
```
You can see this [answer](https://stackoverflow.com/a/1445507/1371471)
> brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.
Update:
In the case of you want a sequence of custom increment, the man page of `seq` gives the following:
```
seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
```
Therefore you can use `seq 1 3 $max` to get a sequence with increment 3.
In general,
```
#!/bin/sh
max=10
incr=3
for i in $(seq 1 $incr $max)
do
echo "$i"
done
```
[](https://stackoverflow.com/users/-1/community)
answered Jul 26, 2013 at 3:08
[](https://stackoverflow.com/users/1371471/lazywei)
1 Comment
[](https://stackoverflow.com/users/1173542/leonid-volnitsky)
This is slow in comparison to built-in command. See my answer.
2013-08-06T18:44:40.373Z+00:00
1
Sequence expressions of the form `{x..y}` only take place when `x` and `y` are literal numbers or single characters. It takes place before variable expansion. If the limits can include variables, use the `seq` command:
```
for i in $(seq 1 $max)
```
answered Jul 26, 2013 at 3:04
[](https://stackoverflow.com/users/1491895/barmar)
[Barmar](https://stackoverflow.com/users/1491895/barmar)
790k58 gold badges555 silver badges670 bronze badges
Comments
1
The bash man page says this:
> Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly tex- tual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
So it appears that bash just passes through verbatim whatever text is in the braces. I wasn't familiar with that syntax, and I had to look it up. It doesn't sound like it was intended to be used in for loops.
bash has newer ways of doing this, but the traditional way is
```
for i in $(seq 1 $max)
do
# whatever...
done
```
Since pretty much anything can be done in bash with enough effort, and I couldn't turn down the challenge, here's how you could do it with braces anyway:
```
for i in $(eval echo {1..$max})
do
echo $i
done
```
answered Jul 26, 2013 at 3:20
[](https://stackoverflow.com/users/1951747/user1951747)
1 Comment
[](https://stackoverflow.com/users/612462/adrian-fr%C3%BChwirth)
That `eval` is not needed.
2013-07-29T08:41:26.29Z+00:00
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://stackoverflow.com/questions/ask)
Explore related questions
See similar questions with these tags. |
| Shard | 169 (laksa) |
| Root Hash | 714406497480128969 |
| Unparsed URL | com,stackoverflow!/questions/17872159/for-loop-in-unix s443 |