How do I iterate over a range of numbers in Bash when the range is given by a variable?
I know I can do this (called "sequence expression" in the Bash documentation):
for i in {1..5}; do echo $i; done
Which gives:
1
2
3
4
5
Yet, how can I replace either of the range endpoints with a variable? This doesn't work:
END=5
for i in {1..$END}; do echo $i; done
Which prints:
{1..5}
I know this question is about
bash
, but - just for the record -ksh93
is smarter and implements it as expected:edit: I prefer
seq
over the other methods because I can actually remember it ;)If you want to stay as close as possible to the brace-expression syntax, try out the
range
function from bash-tricks'range.bash
.For example, all of the following will do the exact same thing as
echo {1..10}
:It tries to support the native bash syntax with as few "gotchas" as possible: not only are variables supported, but the often-undesirable behavior of invalid ranges being supplied as strings (e.g.
for i in {1..a}; do echo $i; done
) is prevented as well.The other answers will work in most cases, but they all have at least one of the following drawbacks:
seq
is a binary which must be installed to be used, must be loaded by bash, and must contain the program you expect, for it to work in this case. Ubiquitous or not, that's a lot more to rely on than just the Bash language itself.{a..z}
; brace expansion will. The question was about ranges of numbers, though, so this is a quibble.{1..10}
brace-expanded range syntax, so programs that use both may be a tiny bit harder to read.$END
variable is not a valid range "bookend" for the other side of the range. IfEND=a
, for example, an error will not occur and the verbatim value{1..a}
will be echoed. This is the default behavior of Bash, as well--it is just often unexpected.Disclaimer: I am the author of the linked code.
Replace
{}
with(( ))
:Yields:
This works fine in
bash
:Here is why the original expression didn't work.
From man bash:
So, brace expansion is something done early as a purely textual macro operation, before parameter expansion.
Shells are highly optimized hybrids between macro processors and more formal programming languages. In order to optimize the typical use cases, the language is made rather more complex and some limitations are accepted.
I would suggest sticking with Posix1 features. This means using
for i in <list>; do
, if the list is already known, otherwise, usewhile
orseq
, as in:1. Bash is a great shell and I use it interactively, but I don't put bash-isms into my scripts. Scripts might need a faster shell, a more secure one, a more embedded-style one. They might need to run on whatever is installed as /bin/sh, and then there are all the usual pro-standards arguments. Remember shellshock, aka bashdoor?