How can I iterate through a simple range of ints using a for loop in ksh?
For example, my script currently does this...
for i in 1 2 3 4 5 6 7
do
#stuff
done
...but I'd like to extend the range way above 7. Is there a better syntax?
How can I iterate through a simple range of ints using a for loop in ksh?
For example, my script currently does this...
for i in 1 2 3 4 5 6 7
do
#stuff
done
...but I'd like to extend the range way above 7. Is there a better syntax?
seq - but only available on linux.
there are other options for seq. But the other solutions are very nice and more important, portable. Thx
ksh93, Bash and zsh all understand C-like
for
loop syntax:Unfortunately, while ksh and zsh understand the curly brace range syntax with constants and variables, Bash only handles constants (including Bash 4).
Just a few examples I use in AIX because there is no range operator or seq, abusing perl instead.
Here's a for loop, using perl like seq:
This is similar, but I prefer while read loops over for. No backticks or issues with spaces.
My fav, do bash-like shell globbing, in this case permutations with perl.
The following will work on AIX / Linux / Solaris ksh.
Optionally if you wanted to pad to 5 places, i.e. 00100 .. 00199 you could begin with:
-Scott
on OpenBSD, use jot:
Curly brackets?