Shell script “for” loop syntax

前端 未结 11 922
攒了一身酷
攒了一身酷 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 07:39

    These all do {1..8} and should all be POSIX. They also will not break if you put a conditional continue in the loop. The canonical way:

    f=
    while [ $((f+=1)) -le 8 ]
    do
      echo $f
    done
    

    Another way:

    g=
    while
      g=${g}1
      [ ${#g} -le 8 ]
    do
      echo ${#g}
    done
    

    and another:

    set --
    while
      set $* .
      [ ${#} -le 8 ]
    do
      echo ${#}
    done
    

提交回复
热议问题