【问题标题】:Get sequence of numbers in bash without using seq [duplicate]在不使用 seq 的情况下获取 bash 中的数字序列 [重复]
【发布时间】:2018-11-23 08:47:59
【问题描述】:

我想在 BASH 脚本中循环一系列数字,但绑定的值不是常量。我知道这种语法:

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

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

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

是否有不使用 seq 的 bash 原生方式来做同样的事情?

【问题讨论】:

    标签: bash seq


    【解决方案1】:

    看看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))) 感兴趣,您可以在手册页中找到更多信息。

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 1970-01-01
      • 2017-05-26
      • 2020-04-26
      • 2013-03-25
      • 2019-10-24
      • 2021-09-17
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多