2118

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}

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
eschercycle
  • 22,067
  • 4
  • 21
  • 13
  • 41
    Hi all, the informations and hints I have read here are all really helpful. I think it is best to avoid the use of seq. The reason is that some scripts need to be portable and must run on a wide variety of unix systems, where some commands may not be present. Just to do an example, seq is not present by default on FreeBSD systems. –  Jan 14 '11 at 11:19
  • 2
    Related discusions: [bash for loop: a range of numbers](http://stackoverflow.com/q/17752902/320399) and [unix.stackexchange.com - In bash, is it possible to use an integer variable in the loop control of a for loop?](http://unix.stackexchange.com/q/55392/11917) – blong Jul 24 '15 at 17:31
  • 12
    I don't remember since which version of Bash exactly but this command supports trailing zeros as well. Which sometimes is really helpful. Command `for i in {01..10}; do echo $i; done` would give numbers like `01, 02, 03, ..., 10`. – topr Jun 15 '16 at 09:33
  • 4
    For those like me who just want to iterate over the range of indices of an **array**, the bash way would be: `myarray=('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](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) – PlasmaBinturong Feb 21 '18 at 14:14
  • 1
    Brace expansion is also used for expressions like `{jpg,png,gif}` which isn't directly addressed here, though the answer will be identical. See [Brace expansion with variable? \[duplicate\]](/questions/19432753/brace-expansion-with-variable) which is marked as a duplicate of this one. – tripleee Aug 20 '18 at 05:46
  • freebsd appears to have `seq` now "these days" FWIW... – rogerdpack Mar 12 '21 at 19:09
  • I tried `for i in {1..${END}}; do echo ${i}; done`. This works in terminal command line but not from within a SHELL script, which confuses me. – Phoenix Mu Dec 01 '21 at 22:10
  • 1
    @PhoenixMu, you may run shell script with bash yourscript.sh rather than sh yourscript.sh – Xin Niu May 12 '22 at 18:52
  • @XinNiu Agreed; – William Martens May 12 '22 at 19:05
  • **Pitfall**: if `END` ends up being equal or larger than `START`, e.g. `END=0` and `START=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. – juanmirocks Sep 08 '22 at 15:23

20 Answers20

2346
for i in $(seq 1 $END); do echo $i; done

edit: I prefer seq over the other methods because I can actually remember it ;)

Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • 61
    seq involves the execution of an external command which usually slows things down. This may not matter but it becomes important if you're writing a script to handle lots of data. – paxdiablo Oct 04 '08 at 01:45
  • 57
    Just fine for a one-liner. Pax's solution is fine too, but if performance were really a concern I wouldn't be using a shell script. – eschercycle Oct 04 '08 at 01:49
  • 2
    Good point. I've found myself building a quick'n'dirty ksh script which ended up turning into a monster and I've not wanted to rewrite it given the time I've already spent - but that's probably just me. – paxdiablo Oct 04 '08 at 01:51
  • 22
    seq is called just once to generate the numbers. exec()'ing it shouldn't be significant unless this loop is inside another tight loop. – Javier Oct 04 '08 at 04:40
  • 34
    The external command isn't really relevent: if you're worried about the overhead of running external commands you don't want to be using shell scripts at all, but generally on unix the overhead is low. However, there is the issue of memory usage if END is high. – Mark Baker Oct 06 '08 at 12:53
  • 28
    Note that `seq $END` would suffice, as the default is to start from 1. From `man seq`: "If FIRST or INCREMENT is omitted, it defaults to 1". – fedorqui Aug 05 '14 at 09:06
  • seq outputs number in floating point format. for large number it can be quite annoying... – Shai Sep 28 '14 at 12:12
  • 3
    And if you need formatted numbers (such as 001, 002, etc) you can use the -f option to seq: $(seq -f %03.0f 1 100) – wojtow Oct 27 '16 at 20:54
  • 11
    Be aware that in bash, `{4..1}` gives `4 3 2 1` since the negative "increment" is deduced automatically. Conversely, `seq 4 1` gives nothing, because the default increment is `1`. In such cases, the full command is to be used: `seq 4 -1 1`. – maxime.bochon Dec 07 '16 at 15:33
  • THANK YOU FOR THIS!! Did syntax change? I thought range was defined: 'seq 1 '; – Chris Brocious Feb 26 '21 at 17:28
  • @ChrisBrocious it is a whatever_number. The `$END` is just a shell variable which presumed to be containing some number. – Hi-Angel Nov 08 '22 at 11:48
  • also it works if some variable is string, {1.."2"} will not work – Demetry Pascal Jun 29 '23 at 21:01
720

The seq method is the simplest, but Bash has built-in arithmetic evaluation.

END=5
for ((i=1;i<=END;i++)); do
    echo $i
done
# ==> outputs 1 2 3 4 5 on separate lines

The for ((expr1;expr2;expr3)); construct works just like for (expr1;expr2;expr3) in C and similar languages, and like other ((expr)) cases, Bash treats them as arithmetic.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 2
    One constantly learns. I would prepend a `typeset -i i END` though. In the pre-bash times (i.e. ksh) it made a difference, but computers were much slower then. – tzot Oct 04 '08 at 22:59
  • 113
    This way avoids the memory overhead of a large list, and a dependency on `seq`. Use it! – bobbogo Mar 14 '11 at 20:08
  • 1
    This will not work on Ubuntu due to syntax error on your code. – Marin Sagovac Nov 30 '14 at 14:01
  • 1
    Non-POSIX bash extension. – Ciro Santilli OurBigBook.com Jul 12 '15 at 07:45
  • 5
    @MarinSagovac Make sure to make `#!/bin/bash` the first line of your script. https://wiki.ubuntu.com/DashAsBinSh#My_production_system_has_broken_and_I_just_want_to_get_it_back_up.21 – Melebius Apr 24 '17 at 13:22
  • 13
    just a very short question on that: why ((i=1;i<=END;i++)) AND NOT ((i=1;i<=$END;i++)); why no $ before END? – Baedsch Sep 20 '18 at 10:26
  • 15
    @Baedsch: for the same reason i is not used as $i. bash man page states for arithmetic evaluation: "Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax." – user3188140 Dec 06 '18 at 11:59
  • 6
    I've included this answer in my performance comparison answer below. https://stackoverflow.com/a/54770805/117471 (This is a note to myself to keep track of which ones I have left to do.) I created that answer specifically to address @bobbogo's unqualified claim and those that upvoted it. SPOILER: Memory overhead of a large list is not nearly as bad as the slow performance of c-style loops in bash. Comment there if you have thoughts. Let's not hijack this thread. – Bruno Bronosky Feb 19 '19 at 17:05
  • THIS SOLVED MY problem; Seq is really great! but this is quite REALLY GREAT too! @ephemient THANKS! – William Martens Jan 12 '22 at 10:57
  • Works great for indexing into list. – young_souvlaki Nov 22 '22 at 20:17
218

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).

Community
  • 1
  • 1
tzot
  • 92,761
  • 29
  • 141
  • 204
  • 1
    The only drawback to the C style loop that it cannot use command line arguments, as they begin with "$". – karatedog Jan 09 '12 at 13:15
  • 4
    @karatedog: `for ((i=$1;i<=$2;++i)); do echo $i; done` in a script works fine for me on bash v.4.1.9, so I don't see a problem with command line arguments. Do you mean something else? – tzot Jan 09 '12 at 14:41
  • 1
    It seems that eval solution is faster than built in C-like for: $ time for ((i=1;i<=100000;++i)); do :; done real 0m21.220s user 0m19.763s sys 0m1.203s $ time for i in $(eval echo "{1..100000}"); do :; done; real 0m13.881s user 0m13.536s sys 0m0.152s – Marcin Zaluski Jul 04 '12 at 14:55
  • 3
    Yes, but *eval is evil*... @MarcinZaluski `time for i in $(seq 100000); do :; done` is a lot quicker! – F. Hauri - Give Up GitHub Aug 02 '13 at 04:58
  • The performance must be platform specific since the eval version is quickest on my machine. – Andrew Prock Apr 02 '14 at 23:25
  • Tested all methods, and only they were supported even by MacOS's default `/bin/sh` (the dash, not Bash) – Top-Master Jul 23 '21 at 07:43
118

Here is why the original expression didn't work.

From man bash:

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

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.

Recommendation

I would suggest sticking with Posix1 features. This means using for i in <list>; do, if the list is already known, otherwise, use while or seq, as in:

#!/bin/sh

limit=4

i=1; while [ $i -le $limit ]; do
  echo $i
  i=$(($i + 1))
done
# Or -----------------------
for i in $(seq 1 $limit); do
  echo $i
done


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?
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • 14
    I don't have the power, but I would move this quite a bit up the list, above all the bash navel-gazing but immediately after the C-style for loop and arithmetic evaluation. – mateor Mar 15 '13 at 18:03
  • 2
    An implication is that brace expansion does not save much memory in comparison to `seq` for large ranges. E.g., `echo {1..1000000} | wc` reveals that the echo produces 1 line, a million words, and 6,888,896 bytes. Trying `seq 1 1000000 | wc` yields a million lines, a million words, and 6,888,896 bytes and is also more than seven times faster, as measured by the `time` command. – George Jan 19 '15 at 01:52
  • Note: I had mentioned the POSIX `while` method previously in my answer: http://stackoverflow.com/a/31365662/895245 But glad you agree :-) – Ciro Santilli OurBigBook.com Sep 04 '16 at 20:09
  • I've included this answer in my performance comparison answer below. https://stackoverflow.com/a/54770805/117471 (This is a note to myself to keep track of which ones I have left to do.) – Bruno Bronosky Feb 19 '19 at 16:34
  • @mateor I thought C-style for loop and arithmetic evaluation are the same solution. Am I missing something? – ychz May 03 '20 at 15:49
  • I like either of the C-style for-loop or the while. One feature given up from bash's range-expansion is the zero-padding with {01..99}. However, printf(1) can do the same. Instead of echo, try `printf "%02i\n" $i` in the for loop. Try `printf "%02i\n" $((i++))` for the while loop method. Format string: "0" indicates pad with zero vs space, "2" is field width, "i" for integer, and don't forget your newline :) – JGurtz Jun 09 '20 at 05:34
99

The POSIX way

If you care about portability, use the example from the POSIX standard:

i=2
end=5
while [ $i -le $end ]; do
    echo $i
    i=$(($i+1))
done

Output:

2
3
4
5

Things which are not POSIX:

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • Just had 4 upvotes on this answer, which is highly unusual. If this was posted on some link aggregation website, please give me a link, cheers. – Ciro Santilli OurBigBook.com Sep 04 '16 at 19:46
  • The quote refers to `x`, not the entire expression. `$((x + 1))` is just fine. – chepner Oct 14 '16 at 12:12
  • While not portable, and differing from GNU `seq` (BSD `seq` allows you to set a sequence termination string with `-t`), FreeBSD and NetBSD also have `seq` since 9.0 and 3.0, respectively. – Adrian Günter Apr 23 '18 at 18:31
  • @CiroSantilli @chepner `$((x+1))` and `$((x + 1))` parse exactly the same, as when the parser tokenizes `x+1` it will be split into 3 tokens: `x`, `+`, and `1`. `x` isn't a valid numerical token, but it is a valid variable name token, yet `x+` is not, hence the split. `+` is a valid arithmetic operator token, yet `+1` is not, so the token is again split there. And so forth. – Adrian Günter Apr 23 '18 at 18:37
  • I've included this answer in my performance comparison answer below. https://stackoverflow.com/a/54770805/117471 (This is a note to myself to keep track of which ones I have left to do.) – Bruno Bronosky Feb 19 '19 at 16:34
41

You can use

for i in $(seq $END); do echo $i; done
Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
  • seq involves the execution of an external command which usually slows things down. – paxdiablo Oct 04 '08 at 01:44
  • 13
    It doesn't involve the execution of an external command for each iteration, just once. If the time to launch one external command is an issue, you're using the wrong language. – Mark Baker Oct 06 '08 at 12:57
  • 1
    So is nesting the only case where this matters? I was wondering if there was a performance difference, or some unknown technical side effect? – Sqeaky Jan 09 '12 at 19:07
  • @Squeaky That's a separate question whichtis answered here: http://stackoverflow.com/questions/4708549/whats-the-difference-between-command-and-command-in-shell-programming – tripleee Sep 04 '16 at 19:24
  • I've included this answer in my performance comparison answer below. https://stackoverflow.com/a/54770805/117471 (This is a note to myself to keep track of which ones I have left to do.) – Bruno Bronosky Feb 19 '19 at 16:34
37

Another layer of indirection:

for i in $(eval echo {1..$END}); do
    ∶
bobbogo
  • 14,989
  • 3
  • 48
  • 57
30

I've combined a few of the ideas here and measured performance.

TL;DR Takeaways:

  1. seq and {..} are really fast
  2. for and while loops are slow
  3. $( ) is slow
  4. for (( ; ; )) loops are slower
  5. $(( )) is even slower
  6. Worrying about N numbers in memory (seq or {..}) is silly (at least up to 1 million.)

These are not conclusions. You would have to look at the C code behind each of these to draw conclusions. This is more about how we tend to use each of these mechanisms for looping over code. Most single operations are close enough to being the same speed that it's not going to matter in most cases. But a mechanism like for (( i=1; i<=1000000; i++ )) is many operations as you can visually see. It is also many more operations per loop than you get from for i in $(seq 1 1000000). And that may not be obvious to you, which is why doing tests like this is valuable.

Demos

# show that seq is fast
$ time (seq 1 1000000 | wc)
 1000000 1000000 6888894

real    0m0.227s
user    0m0.239s
sys     0m0.008s

# show that {..} is fast
$ time (echo {1..1000000} | wc)
       1 1000000 6888896

real    0m1.778s
user    0m1.735s
sys     0m0.072s

# Show that for loops (even with a : noop) are slow
$ time (for i in {1..1000000} ; do :; done | wc)
       0       0       0

real    0m3.642s
user    0m3.582s
sys 0m0.057s

# show that echo is slow
$ time (for i in {1..1000000} ; do echo $i; done | wc)
 1000000 1000000 6888896

real    0m7.480s
user    0m6.803s
sys     0m2.580s

$ time (for i in $(seq 1 1000000) ; do echo $i; done | wc)
 1000000 1000000 6888894

real    0m7.029s
user    0m6.335s
sys     0m2.666s

# show that C-style for loops are slower
$ time (for (( i=1; i<=1000000; i++ )) ; do echo $i; done | wc)
 1000000 1000000 6888896

real    0m12.391s
user    0m11.069s
sys     0m3.437s

# show that arithmetic expansion is even slower
$ time (i=1; e=1000000; while [ $i -le $e ]; do echo $i; i=$(($i+1)); done | wc)
 1000000 1000000 6888896

real    0m19.696s
user    0m18.017s
sys     0m3.806s

$ time (i=1; e=1000000; while [ $i -le $e ]; do echo $i; ((i=i+1)); done | wc)
 1000000 1000000 6888896

real    0m18.629s
user    0m16.843s
sys     0m3.936s

$ time (i=1; e=1000000; while [ $i -le $e ]; do echo $((i++)); done | wc)
 1000000 1000000 6888896

real    0m17.012s
user    0m15.319s
sys     0m3.906s

# even a noop is slow
$ time (i=1; e=1000000; while [ $((i++)) -le $e ]; do :; done | wc)
       0       0       0

real    0m12.679s
user    0m11.658s
sys 0m1.004s
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
  • 1
    Nice! Don't agree with your summary though. Looks to me like `$(seq)` is about the same speed as `{a..b}`. Also, each operation takes about the same time, so adds about 4μs to each iteration of the loop for me. Here an operation is an _echo_ in the body, an arithmetic compare, an increment, etc. Is any of this surprising? Who cares how long it takes the loop paraphernalia to do its job—the runtime is likely to be dominated by the contents of the loop. – bobbogo Feb 20 '19 at 10:46
  • @bobbogo you are right, it really is about operation count. I updated my answer to reflect this. Many calls we make actually perform more operations than we might expect. I narrowed this down from a list of about 50 tests I ran. I expected that my research was too nerdy even for this crowd. As always, I suggest prioritizing your coding efforts like so: Make it shorter; Make it readable; Make it faster; Make it portable. Often #1 causes #3. Don't waste your time on #4 until you must. – Bruno Bronosky Feb 20 '19 at 13:54
  • That's an interesting exercise, though the initial question is about using a variable-countrol iteration, which for example {..} doesn'T allow. – logicOnAbstractions Nov 13 '20 at 17:10
  • I never knew for {i..n} ! That's so cool, every language should have this. – foxesque Mar 30 '22 at 06:26
25

If you need it prefix than you might like this

 for ((i=7;i<=12;i++)); do echo `printf "%2.0d\n" $i |sed "s/ /0/"`;done

that will yield

07
08
09
10
11
12
hossbear
  • 251
  • 3
  • 4
21

If you're on BSD / OS X you can use jot instead of seq:

for i in $(jot $END); do echo $i; done
jefeveizen
  • 484
  • 5
  • 5
  • macOS has `seq` – dcow Mar 15 '21 at 17:11
  • `The seq command first appeared in Plan 9 from Bell Labs. A seq command appeared in NetBSD 3.0, and ported to FreeBSD 9.0. This command was based on the command of the same name in Plan 9 from Bell Labs and the GNU core utilities. The GNU seq command first appeared in the 1.13 shell utilities release.` – dcow Mar 15 '21 at 17:13
20

This works fine in bash:

END=5
i=1 ; while [[ $i -le $END ]] ; do
    echo $i
    ((i = i + 1))
done
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
15

There are many ways to do this, however the ones I prefer is given below

Using seq

Synopsis from man seq

$ seq [-w] [-f format] [-s string] [-t string] [first [incr]] last

Syntax

Full command
seq first incr last

  • first is starting number in the sequence [is optional, by default:1]
  • incr is increment [is optional, by default:1]
  • last is the last number in the sequence

Example:

$ seq 1 2 10
1 3 5 7 9

Only with first and last:

$ seq 1 5
1 2 3 4 5

Only with last:

$ seq 5
1 2 3 4 5

Using {first..last..incr}

Here first and last are mandatory and incr is optional

Using just first and last

$ echo {1..5}
1 2 3 4 5

Using incr

$ echo {1..10..2}
1 3 5 7 9

You can use this even for characters like below

$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26
8

I know this question is about bash, but - just for the record - ksh93 is smarter and implements it as expected:

$ ksh -c 'i=5; for x in {1..$i}; do echo "$x"; done'
1
2
3
4
5
$ ksh -c 'echo $KSH_VERSION'
Version JM 93u+ 2012-02-29

$ bash -c 'i=5; for x in {1..$i}; do echo "$x"; done'
{1..5}
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
8

This is another way:

end=5
for i in $(bash -c "echo {1..${end}}"); do echo $i; done
Jahid
  • 21,542
  • 10
  • 90
  • 108
8

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}:

source range.bash
one=1
ten=10

range {$one..$ten}
range $one $ten
range {1..$ten}
range {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:

  • Many of them use subshells, which can harm performance and may not be possible on some systems.
  • Many of them rely on external programs. Even 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.
  • Solutions that do use only native Bash functionality, like @ephemient's, will not work on alphabetic ranges, like {a..z}; brace expansion will. The question was about ranges of numbers, though, so this is a quibble.
  • Most of them aren't visually similar to the {1..10} brace-expanded range syntax, so programs that use both may be a tiny bit harder to read.
  • @bobbogo's answer uses some of the familiar syntax, but does something unexpected if the $END variable is not a valid range "bookend" for the other side of the range. If END=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.

Zac B
  • 3,796
  • 3
  • 35
  • 52
7

Replace {} with (( )):

tmpstart=0;
tmpend=4;

for (( i=$tmpstart; i<=$tmpend; i++ )) ; do 
echo $i ;
done

Yields:

0
1
2
3
4
sth
  • 222,467
  • 53
  • 283
  • 367
  • 1
    I've included this answer in my performance comparison answer below. https://stackoverflow.com/a/54770805/117471 (This is a note to myself to keep track of which ones I have left to do.) – Bruno Bronosky Feb 19 '19 at 16:26
7

These are all nice but seq is supposedly deprecated and most only work with numeric ranges.

If you enclose your for loop in double quotes, the start and end variables will be dereferenced when you echo the string, and you can ship the string right back to BASH for execution. $i needs to be escaped with \'s so it is NOT evaluated before being sent to the subshell.

RANGE_START=a
RANGE_END=z
echo -e "for i in {$RANGE_START..$RANGE_END}; do echo \\${i}; done" | bash

This output can also be assigned to a variable:

VAR=`echo -e "for i in {$RANGE_START..$RANGE_END}; do echo \\${i}; done" | bash`

The only "overhead" this should generate should be the second instance of bash so it should be suitable for intensive operations.

sjngm
  • 12,423
  • 14
  • 84
  • 114
SuperBob
  • 87
  • 1
  • 1
6

If you're doing shell commands and you (like I) have a fetish for pipelining, this one is good:

seq 1 $END | xargs -I {} echo {}

Alex Spangher
  • 977
  • 2
  • 13
  • 22
3

if you don't wanna use 'seq' or 'eval' or jot or arithmetic expansion format eg. for ((i=1;i<=END;i++)), or other loops eg. while, and you don't wanna 'printf' and happy to 'echo' only, then this simple workaround might fit your budget:

a=1; b=5; d='for i in {'$a'..'$b'}; do echo -n "$i"; done;' echo "$d" | bash

PS: My bash doesn't have 'seq' command anyway.

Tested on Mac OSX 10.6.8, Bash 3.2.48

Zimba
  • 2,854
  • 18
  • 26
0

This works in Bash and Korn, also can go from higher to lower numbers. Probably not fastest or prettiest but works well enough. Handles negatives too.

function num_range {
   # Return a range of whole numbers from beginning value to ending value.
   # >>> num_range start end
   # start: Whole number to start with.
   # end: Whole number to end with.
   typeset s e v
   s=${1}
   e=${2}
   if (( ${e} >= ${s} )); then
      v=${s}
      while (( ${v} <= ${e} )); do
         echo ${v}
         ((v=v+1))
      done
   elif (( ${e} < ${s} )); then
      v=${s}
      while (( ${v} >= ${e} )); do
         echo ${v}
         ((v=v-1))
      done
   fi
}

function test_num_range {
   num_range 1 3 | egrep "1|2|3" | assert_lc 3
   num_range 1 3 | head -1 | assert_eq 1
   num_range -1 1 | head -1 | assert_eq "-1"
   num_range 3 1 | egrep "1|2|3" | assert_lc 3
   num_range 3 1 | head -1 | assert_eq 3
   num_range 1 -1 | tail -1 | assert_eq "-1"
}
Ethan Post
  • 3,020
  • 3
  • 27
  • 27