Variable in for loop digits
本问题已经有最佳答案,请猛点这里访问。
如何在for循环数字中使用变量?
例如:
1 2 3 4 | num="12" for i in {0..$num}; do ... done |
使用变量进行扩展扩展并不像预期的那样工作(有关详细信息,请参见附录B),即
尝试
1 2 3 4 | num="12" for i in $(seq 0 $num); do echo $i done |
附录B:多汁的细节
bash手册说,
The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and filename expansion.
在shell扩展
测试:
1 2 3 4 5 6 7 8 9 | $ num=5 $ for i in {1..$num}; do echo"$i"; done {1..5} $ for i in $(eval echo {1..$num}); do echo"$i"; done 1 2 3 4 5 |
请注意: