βΉοΈ 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.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://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script |
| Last Crawled | 2026-04-02 05:50:41 (9 days ago) |
| First Indexed | 2015-05-31 10:46:39 (10 years ago) |
| HTTP Status Code | 200 |
| Meta Title | for loop syntax in shell script - Ask Ubuntu |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | Thought I'd contribute to the discussion a little bit.
It's already been mentioned that the syntax used in your script is a bashism and hence isn't portable, even though this is C-like and is well understood by those familiar with java and C. From asking my own
question
on unix.stackexchange.com , I've also learned that syntax such as
for i in $(seq 1 5)
generates a set of numbers first and then iterates , which can be wasteful if you have a very large set.
A better way to simulate C-like behavior, which is portable, is to use
while
loop, with a variable that can be incremented. For instance,
#!/bin/sh
number=0
while
[
"
$number
"
-lt 10 ]
do
printf
"\t%d"
"
$number
"
number=`
expr
$number
+ 1 `
done
This works with bash, dash, zsh, ksh, mksh . . . or basically any shell related to bourne shell. In fact, I've a Unix System V book from like 1995, and I've tested their example for bourne shell and it still works. The behaviour is also that of C-like for loop: you have initial condition, testing condition within
while [ . . .]
, and update condition at the end.
csh and tcsh
have syntax closer to C language, but its not portable to other shells, and they're
not recommended
to be used in scripting.
Addition
:
Concerning portability of $RANDOM, according to
this page
on ubuntu wiki, with dash random number generation should rely on using
/dev/urandom |
| Markdown | # 
By clicking βSign upβ, you agree to our [terms of service](https://askubuntu.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://askubuntu.com/legal/privacy-policy).
# OR
Already have an account? [Log in](https://askubuntu.com/users/login)
- [Ubuntu](http://www.ubuntu.com/)
- [Community](http://community.ubuntu.com/)
- [Ask\!](http://askubuntu.com/)
- [Developer](http://developer.ubuntu.com/)
- [Design](http://design.ubuntu.com/)
- [Hardware](http://www.ubuntu.com/certification)
- [Insights](http://insights.ubuntu.com/)
- [Juju](https://jujucharms.com/)
- [Shop](http://shop.ubuntu.com/)
- [More βΊ](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script)
- [Apps](http://apps.ubuntu.com/)
- [Help](https://help.ubuntu.com/)
- [Forum](http://ubuntuforums.org/)
- [Launchpad](http://www.launchpad.net/)
- [MAAS](http://maas.ubuntu.com/)
- [Canonical](http://www.canonical.com/)
[Skip to main content](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#content)
#### Stack Exchange Network
Stack Exchange network consists of 183 Q\&A communities including [Stack Overflow](https://stackoverflow.com/), the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
[Visit Stack Exchange](https://stackexchange.com/)
1. - [Tour Start here for a quick overview of the site](https://askubuntu.com/tour)
- [Help Center Detailed answers to any questions you might have](https://askubuntu.com/help)
- [Meta Discuss the workings and policies of this site](https://meta.askubuntu.com/)
- [About Us Learn more about Stack Overflow the company, and our products](https://stackoverflow.co/)
2. ### [current community](https://askubuntu.com/)
- [Ask Ubuntu](https://askubuntu.com/)
[help](https://askubuntu.com/help) [chat](https://chat.stackexchange.com/?tab=site&host=askubuntu.com)
- [Ask Ubuntu Meta](https://meta.askubuntu.com/)
### your communities
[Sign up](https://askubuntu.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Faskubuntu.com%2Fquestions%2F621981%2Ffor-loop-syntax-in-shell-script) or [log in](https://askubuntu.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Faskubuntu.com%2Fquestions%2F621981%2Ffor-loop-syntax-in-shell-script) to customize your list.
### [more stack exchange communities](https://stackexchange.com/sites)
[company blog](https://stackoverflow.blog/)
3. [Log in](https://askubuntu.com/users/login?ssrc=head&returnurl=https%3A%2F%2Faskubuntu.com%2Fquestions%2F621981%2Ffor-loop-syntax-in-shell-script)
4. [Sign up](https://askubuntu.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Faskubuntu.com%2Fquestions%2F621981%2Ffor-loop-syntax-in-shell-script)
[](https://askubuntu.com/)
1. 1. [Home](https://askubuntu.com/)
2. [Questions](https://askubuntu.com/questions)
3. [Unanswered](https://askubuntu.com/unanswered)
4. [AI Assist](https://stackoverflow.com/ai-assist)
5. [Tags](https://askubuntu.com/tags)
6. [Chat](https://chat.stackexchange.com/)
7. [Users](https://askubuntu.com/users)
8. [Companies](https://stackoverflow.com/jobs/companies?so_medium=askubuntu&so_source=SiteNav)
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=askubuntu-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=askubuntu-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=askubuntu-community&utm_campaign=side-bar&utm_content=explore-teams-compact)
**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=askubuntu-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover)
# [for loop syntax in shell script \[duplicate\]](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script)
[Ask Question](https://askubuntu.com/questions/ask)
Asked
10 years, 10 months ago
Modified [10 years, 10 months ago](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script?lastactivity "2015-05-10 21:32:50Z")
Viewed 86k times
This question shows research effort; it is useful and clear
8
Save this question.
Show activity on this post.
**This question already has answers here**:
[What is the difference between ./ and sh to run a script?](https://askubuntu.com/questions/22910/what-is-the-difference-between-and-sh-to-run-a-script) (6 answers)
Closed 10 years ago.
I run below script:
```
for (( i=1; i <= 5; i++ ))
do
echo "Random number $i: $RANDOM"
done
```
I am getting below Error:
```
Syntax error: Bad for loop variable
```
why this syntax is not working?
- [scripts](https://askubuntu.com/questions/tagged/scripts "show questions tagged 'scripts'")
[Share](https://askubuntu.com/q/621981 "Short permalink to this question")
Share a link to this question
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this question](https://askubuntu.com/posts/621981/edit)
Follow
Follow this question to receive notifications
[edited May 10, 2015 at 19:31](https://askubuntu.com/posts/621981/revisions "show all edits to this post")
[](https://askubuntu.com/users/367165/a-b)
[A.B.](https://askubuntu.com/users/367165/a-b)
92\.5k2222 gold badges255255 silver badges330330 bronze badges
asked May 10, 2015 at 18:12
[](https://askubuntu.com/users/398408/akshay-patel)
[Akshay Patel](https://askubuntu.com/users/398408/akshay-patel)
34522 gold badges44 silver badges77 bronze badges
8
- When I copy-paste this into my terminal, it works as inteded. Do you have something else stored in `i` or have a file named "i" in your WD?
s3lph
β [s3lph](https://askubuntu.com/users/194948/s3lph "14,744 reputation")
2015-05-10 18:15:25 +00:00
[Commented May 10, 2015 at 18:15](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880222_621981)
- 1
You are probably running it in `sh` ..the syntax is correct for `bash` ..how are you running it?
heemayl
β [heemayl](https://askubuntu.com/users/216503/heemayl "94,477 reputation")
2015-05-10 18:15:58 +00:00
[Commented May 10, 2015 at 18:15](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880223_621981)
- 1
What command are you using to run the script?
heemayl
β [heemayl](https://askubuntu.com/users/216503/heemayl "94,477 reputation")
2015-05-10 18:19:25 +00:00
[Commented May 10, 2015 at 18:19](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880227_621981)
- 1
As far as your previous records are concerned, you should [read this](http://stackoverflow.com/help/someone-answers)
heemayl
β [heemayl](https://askubuntu.com/users/216503/heemayl "94,477 reputation")
2015-05-10 18:46:23 +00:00
[Commented May 10, 2015 at 18:46](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880245_621981)
- 1
You may be interested in an answer by a user posted on my question here: [unix.stackexchange.com/questions/198985/β¦](http://unix.stackexchange.com/questions/198985/how-to-make-mksh-for-loop-go-from-1-to-n "how to make mksh for loop go from 1 to n")
Sergiy Kolodyazhnyy
β [Sergiy Kolodyazhnyy](https://askubuntu.com/users/295286/sergiy-kolodyazhnyy "108,386 reputation")
2015-05-10 19:49:31 +00:00
[Commented May 10, 2015 at 19:49](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880289_621981)
\| [Show **3** more comments](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script "Expand to show all comments on this post")
## 3 Answers 3
Sorted by:
[Reset to default](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script?answertab=scoredesc#tab-top)
This answer is useful
14
Save this answer.
Show activity on this post.
As heemayl's answer points out, your construction is a bashism. The standard way of doing what you want would be:
```
for i in 1 2 3 4 5
do
echo "Random number $i: $RANDOM"
done
```
Or, more easily expandably:
```
for i in $(seq 1 5)
do
echo "Random number $i: $RANDOM"
done
```
(See `man seq` for more detail).
[Share](https://askubuntu.com/a/621986 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://askubuntu.com/posts/621986/edit)
Follow
Follow this answer to receive notifications
[edited May 10, 2015 at 18:44](https://askubuntu.com/posts/621986/revisions "show all edits to this post")
answered May 10, 2015 at 18:36
[](https://askubuntu.com/users/112879/evilsoup)
[evilsoup](https://askubuntu.com/users/112879/evilsoup)
4,62511 gold badge2323 silver badges2626 bronze badges
6
- Brace expansion is also a bashism.
muru
β [muru](https://askubuntu.com/users/158442/muru "209,415 reputation") β¦
2015-05-10 18:38:17 +00:00
[Commented May 10, 2015 at 18:38](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880237_621986)
- 1
`for i in 1 2 3 4 5` is kind of trolling....use `seq 1 5`..
heemayl
β [heemayl](https://askubuntu.com/users/216503/heemayl "94,477 reputation")
2015-05-10 18:41:07 +00:00
[Commented May 10, 2015 at 18:41](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880238_621986)
- @heemayl -- Not trolling, I just had to double-check the seq syntax before I included it
evilsoup
β [evilsoup](https://askubuntu.com/users/112879/evilsoup "4,625 reputation")
2015-05-10 18:45:35 +00:00
[Commented May 10, 2015 at 18:45](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880244_621986)
- @heemayl no trolling, some users don't know all the minute details of shell scripting =)
Sergiy Kolodyazhnyy
β [Sergiy Kolodyazhnyy](https://askubuntu.com/users/295286/sergiy-kolodyazhnyy "108,386 reputation")
2015-05-10 19:48:33 +00:00
[Commented May 10, 2015 at 19:48](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880286_621986)
- 3
Not that one can say `seq` is all that standard either. It's not POSIX.
muru
β [muru](https://askubuntu.com/users/158442/muru "209,415 reputation") β¦
2015-05-10 19:49:25 +00:00
[Commented May 10, 2015 at 19:49](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880287_621986)
\| [Show **1** more comment](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script "Expand to show all comments on this post")
This answer is useful
6
Save this answer.
Show activity on this post.
Your original script has a `(( i=1; i <= 5; i++ ))` construct in `for` loop which is a bashism and hence is not being understood by `dash`.
In Ubuntu `sh` is a symbolic link to `dash`, so when you are running
```
sh ./script.sh
```
you are basically running
```
dash ./script.sh
```
As i mentioned earlier `dash` is not understanding the C-like `for` loop construct, it is showing the error.
To run the script using `bash`, you can:
- Run it as as an argument to `bash` e.g. `bash script.sh` (you don't need to make the script executable)
- Make the script executable and from a `bash` shell run (from the directory containing the script):
```
./script.sh
```
- The most portable way is to use a shebang (`#!/bin/bash` or preferably `#!/usr/bin/env bash`) as the first line of your script. In this way you can run the script from any shell by `./script.sh`, the script will be interpreted as a `bash` script and hence will be executed accordingly.
[Share](https://askubuntu.com/a/621983 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://askubuntu.com/posts/621983/edit)
Follow
Follow this answer to receive notifications
[edited May 10, 2015 at 21:32](https://askubuntu.com/posts/621983/revisions "show all edits to this post")
answered May 10, 2015 at 18:25
[](https://askubuntu.com/users/216503/heemayl)
[heemayl](https://askubuntu.com/users/216503/heemayl)
94\.5k2323 gold badges216216 silver badges278278 bronze badges
2
- For even greater portability, a bash script can start with `#!/usr/bin/env bash`. Although not 100% of Unix-like systems have `env` in `/usr/bin`, those that don't are rarer than those with `bash` not in `bin`. (`bin` doesn't have `bash` on most systems where `bash` is installed but not part of the base system, e.g., FreeBSD.)
Eliah Kagan
β [Eliah Kagan](https://askubuntu.com/users/22949/eliah-kagan "120,140 reputation")
2015-05-10 21:16:01 +00:00
[Commented May 10, 2015 at 21:16](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880341_621983)
- @EliahKagan Yes..i forgot to mention `env`..that will be most portable..
heemayl
β [heemayl](https://askubuntu.com/users/216503/heemayl "94,477 reputation")
2015-05-10 21:29:09 +00:00
[Commented May 10, 2015 at 21:29](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880353_621983)
[Add a comment](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script "Use comments to ask for more information or suggest improvements. Avoid comments like β+1β or βthanksβ.") \|
This answer is useful
5
Save this answer.
Show activity on this post.
Thought I'd contribute to the discussion a little bit.
It's already been mentioned that the syntax used in your script is a bashism and hence isn't portable, even though this is C-like and is well understood by those familiar with java and C. From asking my own [question](https://unix.stackexchange.com/q/198985/85039) on unix.stackexchange.com , I've also learned that syntax such as `for i in $(seq 1 5)` generates a set of numbers first and then iterates , which can be wasteful if you have a very large set.
A better way to simulate C-like behavior, which is portable, is to use `while` loop, with a variable that can be incremented. For instance,
```
#!/bin/sh
number=0
while [ "$number" -lt 10 ]
do
printf "\t%d" "$number"
number=`expr $number + 1 `
done
```
This works with bash, dash, zsh, ksh, mksh . . . or basically any shell related to bourne shell. In fact, I've a Unix System V book from like 1995, and I've tested their example for bourne shell and it still works. The behaviour is also that of C-like for loop: you have initial condition, testing condition within `while [ . . .]`, and update condition at the end.
[csh and tcsh](http://www.cyberciti.biz/faq/csh-shell-scripting-loop-example/) have syntax closer to C language, but its not portable to other shells, and they're [not recommended](http://www.grymoire.com/unix/CshTop10.txt) to be used in scripting.
**Addition**:
Concerning portability of \$RANDOM, according to [this page](https://wiki.ubuntu.com/DashAsBinSh#A.24RANDOM) on ubuntu wiki, with dash random number generation should rely on using `/dev/urandom`
[Share](https://askubuntu.com/a/622034 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://askubuntu.com/posts/622034/edit)
Follow
Follow this answer to receive notifications
[edited Apr 13, 2017 at 12:37](https://askubuntu.com/posts/622034/revisions "show all edits to this post")
[](https://askubuntu.com/users/-1/community)
[Community](https://askubuntu.com/users/-1/community)Bot
1
answered May 10, 2015 at 20:35
[](https://askubuntu.com/users/295286/sergiy-kolodyazhnyy)
[Sergiy Kolodyazhnyy](https://askubuntu.com/users/295286/sergiy-kolodyazhnyy)
108k2121 gold badges295295 silver badges528528 bronze badges
1
- 1
This is indeed much more efficient when the loop variable can take on many values. For example, on my system, using `command time -f '%E' dash -c '...'` as the test harness, `for i in $(seq 1 100000000); do test $i -eq 1000 && break; done` took 0:48.37, but `i=1; while [ $i -lt 100000000 ]; do test $i -eq 1000 && break; i=$(expr $i + 1); done` took 0:02.23.
Eliah Kagan
β [Eliah Kagan](https://askubuntu.com/users/22949/eliah-kagan "120,140 reputation")
2015-05-10 21:13:01 +00:00
[Commented May 10, 2015 at 21:13](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script#comment880337_622034)
[Add a comment](https://askubuntu.com/questions/621981/for-loop-syntax-in-shell-script "Use comments to ask for more information or suggest improvements. Avoid comments like β+1β or βthanksβ.") \|
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://askubuntu.com/questions/ask)
Explore related questions
- [scripts](https://askubuntu.com/questions/tagged/scripts "show questions tagged 'scripts'")
See similar questions with these tags.
- Featured on Meta
- [Should the Community Bot stop bumping questions from EOL releases?](https://meta.askubuntu.com/questions/20776/should-the-community-bot-stop-bumping-questions-from-eol-releases?cb=1)
#### Linked
[82](https://askubuntu.com/questions/22910/what-is-the-difference-between-and-sh-to-run-a-script?lq=1 "Question score (upvotes - downvotes)")
[What is the difference between ./ and sh to run a script?](https://askubuntu.com/questions/22910/what-is-the-difference-between-and-sh-to-run-a-script?noredirect=1&lq=1)
#### Related
[0](https://askubuntu.com/questions/264949/grant-command-syntax-error-near-password-in-shell-script?rq=1 "Question score (upvotes - downvotes)")
[grant command syntax error near password in shell script](https://askubuntu.com/questions/264949/grant-command-syntax-error-near-password-in-shell-script?rq=1)
[1](https://askubuntu.com/questions/692378/why-does-my-script-return-a-syntax-error?rq=1 "Question score (upvotes - downvotes)")
[Why does my script return a syntax error?](https://askubuntu.com/questions/692378/why-does-my-script-return-a-syntax-error?rq=1)
[4](https://askubuntu.com/questions/712990/using-user-input-to-determine-how-many-times-a-for-loop-iterates?rq=1 "Question score (upvotes - downvotes)")
[Using user input to determine how many times a for loop iterates](https://askubuntu.com/questions/712990/using-user-input-to-determine-how-many-times-a-for-loop-iterates?rq=1)
[0](https://askubuntu.com/questions/787421/running-a-do-loop?rq=1 "Question score (upvotes - downvotes)")
[Running a do loop](https://askubuntu.com/questions/787421/running-a-do-loop?rq=1)
[8](https://askubuntu.com/questions/895586/syntax-error-in-for-loop-when-redirecting-output-to-file-in-background?rq=1 "Question score (upvotes - downvotes)")
[Syntax error in for loop when redirecting output to file in background](https://askubuntu.com/questions/895586/syntax-error-in-for-loop-when-redirecting-output-to-file-in-background?rq=1)
[0](https://askubuntu.com/questions/1211738/syntax-error-in-conditional-expression-with-bash-script?rq=1 "Question score (upvotes - downvotes)")
[syntax error in conditional expression with bash script](https://askubuntu.com/questions/1211738/syntax-error-in-conditional-expression-with-bash-script?rq=1)
[2](https://askubuntu.com/questions/1332634/why-is-this-sh-script-run-on-centos-but-not-on-ubuntu?rq=1 "Question score (upvotes - downvotes)")
[Why is this .sh script run on CentOS but not on Ubuntu?](https://askubuntu.com/questions/1332634/why-is-this-sh-script-run-on-centos-but-not-on-ubuntu?rq=1)
[0](https://askubuntu.com/questions/1551013/bash-script-vs-command-line-variable-not-working?rq=1 "Question score (upvotes - downvotes)")
[Bash Script vs Command Line - Variable not working](https://askubuntu.com/questions/1551013/bash-script-vs-command-line-variable-not-working?rq=1)
#### [Hot Network Questions](https://stackexchange.com/questions?tab=hot)
- [How formal are expectations for in-office work vs. alternatives (outside, coffee shop, pub) during a PhD in France?](https://academia.stackexchange.com/questions/226304/how-formal-are-expectations-for-in-office-work-vs-alternatives-outside-coffee)
- [Why didn't God lead the Israelites out of Egypt same way Moses came in over the Peninsula](https://hermeneutics.stackexchange.com/questions/115657/why-didnt-god-lead-the-israelites-out-of-egypt-same-way-moses-came-in-over-the)
- [Do most philosophers believe that foreknowledge means that God has to determine everything?](https://philosophy.stackexchange.com/questions/137407/do-most-philosophers-believe-that-foreknowledge-means-that-god-has-to-determine)
- ["Onomat-appear" - a straightforward wordsearch?](https://puzzling.stackexchange.com/questions/137555/onomat-appear-a-straightforward-wordsearch)
- [What is First Airplane in History That Have Max Take Off Weight Bigger That 1 Metric Ton](https://aviation.stackexchange.com/questions/115114/what-is-first-airplane-in-history-that-have-max-take-off-weight-bigger-that-1-me)
- [Handling Profanity Censorship in BERTopic](https://datascience.stackexchange.com/questions/137879/handling-profanity-censorship-in-bertopic)
- [How to construct a re-arming mechanism for a medieval crossbow using medieval technology?](https://worldbuilding.stackexchange.com/questions/273045/how-to-construct-a-re-arming-mechanism-for-a-medieval-crossbow-using-medieval-te)
- [Why does gauge symmetries cause differential operator in action to be singular?](https://physics.stackexchange.com/questions/870715/why-does-gauge-symmetries-cause-differential-operator-in-action-to-be-singular)
- [How to handle calendar year as a continuous predictor with a mismatched train/test time horizon?](https://stats.stackexchange.com/questions/675416/how-to-handle-calendar-year-as-a-continuous-predictor-with-a-mismatched-train-te)
- [Moving SW1 to SW2, does it force SW1 games to upgrade](https://gaming.stackexchange.com/questions/418300/moving-sw1-to-sw2-does-it-force-sw1-games-to-upgrade)
- [Will updating Ubuntu break my Wine installation?](https://askubuntu.com/questions/1565307/will-updating-ubuntu-break-my-wine-installation)
- [Is sampling a signal with 2-channel ADC, where the second channel is delayed by 90Β° at sampling frequency actually I/Q sampling?](https://dsp.stackexchange.com/questions/99743/is-sampling-a-signal-with-2-channel-adc-where-the-second-channel-is-delayed-by)
- [How do I calculate repeatability (ICC) from a beta GLMM with predictors for π?](https://stats.stackexchange.com/questions/675431/how-do-i-calculate-repeatability-icc-from-a-beta-glmm-with-predictors-for)
- [Alternative proof of the Riesz Representation Theorem](https://math.stackexchange.com/questions/5130822/alternative-proof-of-the-riesz-representation-theorem)
- [First SIMD program](https://codereview.stackexchange.com/questions/301729/first-simd-program)
- [How to correctly parse tokens and preserve command-argument structure in expl3?](https://tex.stackexchange.com/questions/761496/how-to-correctly-parse-tokens-and-preserve-command-argument-structure-in-expl3)
- [Courtesy time and key signatures in first ending](https://music.stackexchange.com/questions/143437/courtesy-time-and-key-signatures-in-first-ending)
- [Pullback of Orientation form under a deck transformation](https://math.stackexchange.com/questions/5131190/pullback-of-orientation-form-under-a-deck-transformation)
- [Why is SSH failing on macOS 26.3?](https://apple.stackexchange.com/questions/486167/why-is-ssh-failing-on-macos-26-3)
- [Evolution vs is ought arguments?](https://philosophy.stackexchange.com/questions/137382/evolution-vs-is-ought-arguments)
- [circuitikz, how to prettify connection between transformer and connecting lines?](https://tex.stackexchange.com/questions/761472/circuitikz-how-to-prettify-connection-between-transformer-and-connecting-lines)
- [Productivity vs Enjoyment in PhD research](https://academia.stackexchange.com/questions/226350/productivity-vs-enjoyment-in-phd-research)
- [Was Fermat's Last Theorem known for infinitely many primes before Wiles?](https://mathoverflow.net/questions/509735/was-fermats-last-theorem-known-for-infinitely-many-primes-before-wiles)
- [Designing a clamping circuit / clipper for a piezoelectric disk going into an ADC](https://electronics.stackexchange.com/questions/767643/designing-a-clamping-circuit-clipper-for-a-piezoelectric-disk-going-into-an-ad)

lang-bash
# Why are you flagging this comment?
It contains harassment, bigotry or abuse.
This comment attacks a person or group. Learn more in our [Abusive behavior policy](https://askubuntu.com/conduct/abusive-behavior).
It's unfriendly or unkind.
This comment is rude or condescending. Learn more in our [Code of Conduct](https://askubuntu.com/conduct/abusive-behavior).
Not needed.
This comment is not relevant to the post.
```
```
Enter at least 6 characters
Something else.
A problem not listed above. Try to be as specific as possible.
```
```
Enter at least 6 characters
Flag comment
Cancel
You have 0 flags left today
# 
# Hang on, you can't upvote just yet.
You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation)
Instead, you can save this post to reference later.
Save this post for later
Not now
##### [Ask Ubuntu](https://askubuntu.com/)
- [Tour](https://askubuntu.com/tour)
- [Help](https://askubuntu.com/help)
- [Chat](https://chat.stackexchange.com/?tab=site&host=askubuntu.com)
- [Contact](https://askubuntu.com/contact)
- [Feedback](https://meta.askubuntu.com/)
##### [Company](https://stackoverflow.co/)
- [Stack Overflow](https://stackoverflow.com/)
- [Stack Internal](https://stackoverflow.co/internal/)
- [Stack Data Licensing](https://stackoverflow.co/data-licensing/)
- [Stack Ads](https://stackoverflow.co/advertising/)
- [About](https://stackoverflow.co/)
- [Press](https://stackoverflow.co/company/press/)
- [Legal](https://stackoverflow.com/legal)
- [Privacy Policy](https://stackoverflow.com/legal/privacy-policy)
- [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public)
- 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.4.1.41700
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence. |
| Readable Markdown | Thought I'd contribute to the discussion a little bit.
It's already been mentioned that the syntax used in your script is a bashism and hence isn't portable, even though this is C-like and is well understood by those familiar with java and C. From asking my own [question](https://unix.stackexchange.com/q/198985/85039) on unix.stackexchange.com , I've also learned that syntax such as `for i in $(seq 1 5)` generates a set of numbers first and then iterates , which can be wasteful if you have a very large set.
A better way to simulate C-like behavior, which is portable, is to use `while` loop, with a variable that can be incremented. For instance,
```
#!/bin/sh
number=0
while [ "$number" -lt 10 ]
do
printf "\t%d" "$number"
number=`expr $number + 1 `
done
```
This works with bash, dash, zsh, ksh, mksh . . . or basically any shell related to bourne shell. In fact, I've a Unix System V book from like 1995, and I've tested their example for bourne shell and it still works. The behaviour is also that of C-like for loop: you have initial condition, testing condition within `while [ . . .]`, and update condition at the end.
[csh and tcsh](http://www.cyberciti.biz/faq/csh-shell-scripting-loop-example/) have syntax closer to C language, but its not portable to other shells, and they're [not recommended](http://www.grymoire.com/unix/CshTop10.txt) to be used in scripting.
**Addition**:
Concerning portability of \$RANDOM, according to [this page](https://wiki.ubuntu.com/DashAsBinSh#A.24RANDOM) on ubuntu wiki, with dash random number generation should rely on using `/dev/urandom` |
| Shard | 53 (laksa) |
| Root Hash | 9277990770814737653 |
| Unparsed URL | com,askubuntu!/questions/621981/for-loop-syntax-in-shell-script s443 |