在不使用seq的情况下获取bash中的数字序列

时间:2018-11-23 08:48:00

标签: bash seq

我想遍历BASH脚本中的数字序列,但是绑定值不是常数。我知道这种语法:

for year in {2000..2010}; do
  echo ${year}
done

但是在我的情况下,2000和2010的值正在变化,所以我现在正在做的是:

for year in `seq ${yeari} ${yeare}`; do
  echo ${year}
done

是否有不使用seq的bash本机方式?

1 个答案:

答案 0 :(得分:0)

看看man bash

 for (( expr1 ; expr2 ; expr3 )) ; do list ; done
          First, the arithmetic expression expr1 is evaluated according to
          the rules described  below  under  ARITHMETIC  EVALUATION.   The
          arithmetic  expression  expr2 is then evaluated repeatedly until
          it evaluates to zero.  Each time expr2 evaluates to  a  non-zero
          value,  list  is executed and the arithmetic expression expr3 is
          evaluated.  If any expression is omitted, it behaves  as  if  it
          evaluates to 1.  The return value is the exit status of the last
          command in list that is executed, or false if any of the expres-
          sions is invalid.
for ((i=yeari; i<yeare; i++))
do
  echo $i
done

您可能还对'算术扩展'($((expression)))感兴趣,您可以在手册页中找到更多内容。