discussion
Using seq
is fine, as Jiaaro suggested. Pax Diablo suggested a Bash loop to avoid calling a subprocess, with the additional advantage of being more memory friendly if $END is too large. Zathrus spotted a typical bug in the loop implementation, and also hinted that since i
is a text variable, continuous conversions to-and-fro numbers are performed with an associated slow-down.
integer arithmetic
This is an improved version of the Bash loop:
typeset -i i END
let END=5 i=1
while ((i<=END)); do
echo $i
…
let i++
done
If the only thing that we want is the echo
, then we could write echo $((i++))
.
ephemient taught me something: Bash allows for ((expr;expr;expr))
constructs. Since I've never read the whole man page for Bash (like I've done with the Korn shell (ksh
) man page, and that was a long time ago), I missed that.
So,
typeset -i i END # Let's be explicit
for ((i=1;i<=END;++i)); do echo $i; done
seems to be the most memory-efficient way (it won't be necessary to allocate memory to consume seq
's output, which could be a problem if END is very large), although probably not the “fastest”.
the initial question
eschercycle noted that the {a..b} Bash notation works only with literals; true, accordingly to the Bash manual. One can overcome this obstacle with a single (internal) fork()
without an exec()
(as is the case with calling seq
, which being another image requires a fork+exec):
for i in $(eval echo "{1..$END}"); do
Both eval
and echo
are Bash builtins, but a fork()
is required for the command substitution (the $(…)
construct).
for i in {01..10}; do echo $i; done
would give numbers like01, 02, 03, ..., 10
. – Planogametemyarray=('a' 'b' 'c'); for i in ${!myarray[@]}; do echo $i; done
(note the exclamation mark). It's more specific than the original question, but could help. See bash parameter expansions – Saraband{jpg,png,gif}
which isn't directly addressed here, though the answer will be identical. See Brace expansion with variable? [duplicate] which is marked as a duplicate of this one. – Natchseq
now "these days" FWIW... – Debarfor i in {1..${END}}; do echo ${i}; done
. This works in terminal command line but not from within a SHELL script, which confuses me. – GironEND
ends up being equal or larger thanSTART
, e.g.END=0
andSTART=1
,seq
will generate a non-empty sequence, and accordingly your loop will run at least 1 iteration. In most cases, that's not what you want. It's a bug. – Forewent