Herdsman
Herdsman

Reputation: 859

Bash - how to make arithmetic expansion in range braces?

I want to do this: for i in {1.."$((2**3))"}; do echo "$i"; done

But that would output {1..8}, which I want to execute, not output. How to?

Upvotes: 1

Views: 203

Answers (2)

Jetchisel
Jetchisel

Reputation: 7801

You can't do in like that in bash, brace expansion happens before variable does. A c-style for loop can be an alternative.

for ((i = 1; i <= 2**3; i++)); do printf '%d ' "$i"; done

... Or if you really want to do the brace expansion use eval which is not advised to use but it is the only way...

eval echo {1..$((2**3))}

Upvotes: 1

Abelisto
Abelisto

Reputation: 15624

You could to use seq instead of range braces:

for i in $(seq 1 $((2**3))); do echo "$i"; done

Upvotes: 2

Related Questions