简体   繁体   中英

Bash - how to make arithmetic expansion in range braces?

I want to do this: for i in {1.."$((2**3))"}; do echo "$i"; done 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?

You could to use seq instead of range braces:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM