1. 程式人生 > >shell for循環

shell for循環

clas em1 一行 style str his col ... comm

寫成一行:for var in item1 item2 ... itemN; do command1; command2 done;

#順序輸出

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

結果輸出

The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5

#輸出字符串中的字符

for str in This is a string
do
    echo $str
done

結果輸出

This is a string

例:

#!/bin/bash
for((i=1;i<=5;i++));do
    echo "這是第 $i 次調用";
done;

結果輸出:

這是第1次調用
這是第2次調用
這是第3次調用
這是第4次調用
這是第5次調用

shell for循環