How do I iterate over a range of numbers defined by variables in Bash?

后端 未结 20 1884
予麋鹿
予麋鹿 2020-11-21 05:19

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 documentatio

20条回答
  •  野的像风
    2020-11-21 05:56

    The seq method is the simplest, but Bash has built-in arithmetic evaluation.

    END=5
    for ((i=1;i<=END;i++)); do
        echo $i
    done
    # ==> outputs 1 2 3 4 5 on separate lines
    

    The for ((expr1;expr2;expr3)); construct works just like for (expr1;expr2;expr3) in C and similar languages, and like other ((expr)) cases, Bash treats them as arithmetic.

提交回复
热议问题