Shell script “for” loop syntax

前端 未结 11 928
攒了一身酷
攒了一身酷 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:06

    Try the arithmetic-expression version of for:

    max=10
    for (( i=2; i <= $max; ++i ))
    do
        echo "$i"
    done
    

    This is available in most versions of bash, and should be Bourne shell (sh) compatible also.

提交回复
热议问题