如何在 bash 中生成步骤 n 的范围?(生成带增量的数字序列) [英] How to produce a range with step n in bash? (generate a sequence of numbers with increments)
本文介绍了如何在 bash 中生成步骤 n 的范围?(生成带增量的数字序列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 bash 中迭代一个范围的方法是
The way to iterate over a range in bash is
for i in {0..10}; do echo $i; done
使用步骤迭代序列的语法是什么?比如说,我想在上面的例子中只得到偶数.
What would be the syntax for iterating over the sequence with a step? Say, I would like to get only even number in the above example.
推荐答案
我愿意
for i in `seq 0 2 10`; do echo $i; done
(当然 seq 0 2 10
会自行产生相同的输出).
(though of course seq 0 2 10
will produce the same output on its own).
请注意,seq
允许浮点数(例如,seq .5 .25 3.5
),但 bash 的大括号扩展只允许整数.
Note that seq
allows floating-point numbers (e.g., seq .5 .25 3.5
) but bash's brace expansion only allows integers.
这篇关于如何在 bash 中生成步骤 n 的范围?(生成带增量的数字序列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文