2

Possible Duplicate:
How do I iterate over a range of numbers in bash?

When I do this:

RANGE_COUNT=28
for i in {0..$RANGE_COUNT} ; do     echo $i; done

I get this

{0..28}

When I do this:

for i in {0..5} ; do     echo $i; done

I get this:

0
1
2
3
4
5

What's up with that and how do I make it do what I clearly intend and am obviously not stating correctly?

Community
  • 1
  • 1
Zak
  • 24,947
  • 11
  • 38
  • 68

3 Answers3

4

you can use c style for loop

for((i=1;i<=$RANGE_COUNT;i++))
do
  ...
done

or use eval

for i in $(eval echo {0..$RANGE_COUNT}); do echo $i; done

other methods include while loop

i=0
while [ "$i" -le "$RANGE_COUNT" ]; do echo $i; ((i++)); done
bash-o-logist
  • 6,665
  • 1
  • 17
  • 14
3

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, it's something done early as a purely textual macro expansion, before parameter expansion.

Shells are highly optimized hybrids between macro processors and more formal programming languages. In order to optimize the typical use cases, various compromises are made; sometimes the language gets more complex and sometimes limitations are accepted.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1

Use the arithmetic notation:

for ((i=0; i<$RANGE_COUNT; i++)); do echo $i; done
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152