Shell script “for” loop syntax

前端 未结 11 936
攒了一身酷
攒了一身酷 2020-12-07 07:11

I have gotten the following to work:

for i in {2..10}
do
    echo \"output: $i\"
done

It produces a bunch of lines of output: 2

11条回答
  •  庸人自扰
    2020-12-07 08:05

    Brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.

    Instead, use the seq 2 $max method as user mob stated.

    So, for your example it would be:

    max=10
    for i in `seq 2 $max`
    do
        echo "$i"
    done
    

提交回复
热议问题