【问题标题】:Bash - For loop not working with variables in the declaration [duplicate]Bash - For循环不适用于声明中的变量[重复]
【发布时间】:2018-10-12 16:03:09
【问题描述】:
for i in {0..3}
do
   echo $i
done

返回

0
1
2
3

但是

firstNumber=$1
lastNumber=$2

for i in {$firstNumber..$lastNumber}
do
   echo $i
done

返回,例如,

{0..3}

如何在 For 循环声明中使用变量?

【问题讨论】:

  • 请注意,在 bash 中,{$min..$max} 不起作用。它们必须是静态数字。

标签: bash for-loop variables


【解决方案1】:
for ((i=$firstNumber; i <= $lastNumber; i++)); do echo $i; done

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多