我如何遍历在Bash的变量定义的数字范围? [英] How do I iterate over a range of numbers defined by variables in Bash?
本文介绍了我如何遍历在Bash的变量定义的数字范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何遍历一个范围在击数时的范围由可变定
我知道我可以在Bash的文档):
为我在{1..5};做回声$ I; DONE
这给出了:
1
2
3
4
5
块引用>不过,我怎么能更换任何范围端点有一个变量?这不工作:
END = 5
因为我在{1 .. $ END};做回声$ I; DONE它打印:
{} 1..5
块引用>解决方案因为我在$(SEQ 1 $ END)做回声$ I;做
编辑:我preFER
SEQ
比其他方法,因为其实我可以记住它;)How do I iterate over a range of numbers in Bash when the range is given by a variable?
I know I can do this (called "sequence expression" in the Bash documentation):
for i in {1..5}; do echo $i; done
Which gives:
1
2
3
4
5Yet, how can I replace either of the range endpoints with a variable? This doesn't work:
END=5 for i in {1..$END}; do echo $i; done
Which prints:
{1..5}
解决方案for i in $(seq 1 $END); do echo $i; done
edit: I prefer
seq
over the other methods because I can actually remember it ;)这篇关于我如何遍历在Bash的变量定义的数字范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文