[英]Bash - how to make arithmetic expansion in range braces?
我想这样做: for i in {1.."$((2**3))"}; do echo "$i"; done
for i in {1.."$((2**3))"}; do echo "$i"; done
但这将是 output {1..8}
,我想执行,而不是 output。 如何?
您可以使用seq
而不是范围大括号:
for i in $(seq 1 $((2**3))); do echo "$i"; done
您不能在 bash 中那样做,大括号扩展发生在变量之前。 c 风格的 for 循环可以作为替代方案。
for ((i = 1; i <= 2**3; i++)); do printf '%d ' "$i"; done
...或者,如果您真的想进行大括号扩展,请使用不建议使用的eval
,但这是唯一的方法...
eval echo {1..$((2**3))}
See the local bash manual for the order of expansion PAGER='less +/^EXPANSION' man bash
and the online manual (thanks to @Freddy
) https://www.gnu.org/software/bash/manual/html_node/Shell -扩展。html
参见本地bash手册中的eval
PAGER='less +/^[[:blank:]]*eval\ ' man bash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.