Linux Shell for循环

Shell for 循环分为三种:
1. 列表for循环
2. 不带列表for循环
3. 类C风格的for循环

列表for循环:将一组命令执行已知的次数

语法:

for variable in {list}
do
    command ...
done

Example:

for variable in 1 2 3 4 5 
do
    echo "Hello, Welcome $variable times"
done

for 列表循环支持 列表的 简写方式

for variable in {1..5}
do
    echo "Hello, Welcome $variable times"
done

for循环中的列表,支持 步长,可参考命令[seq] ,以下list可改写为:${seq 1 2 100}

sum=0
for variable in {1..100..2}
do
    let "sum+=variable"
done
echo "sum = $sum"

循环字符串列表

for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do 
    echo "$day"
done
for ((i<0; i<20; i++))
do
echo ${i}
done