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?

1
  • thx.. just bad search fu.. closing question as dup Commented Apr 19, 2011 at 22:22

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

Use the arithmetic notation:

for ((i=0; i<$RANGE_COUNT; i++)); do echo $i; done

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.