语法:
for name [ in [word ... ]]
do
compound-list
done
例如:
#!/bin/bash
for i in {1..3}
do
echo "Welcome $i times"
done
运行结果:
Welcome 1 times
Welcome 2 times
Welcome 3 times
另外一种写法,看上去与C语言类似:
#!/bin/bash
for ((i = 1; i <= 3; i++));
do
echo "Welcome $i times"
done
运行结果和上面是一样的。
还有一种方法,直接在shell命令行中运行:
$ for i in {1..3}; do echo "Welcome $i times"; done
Welcome 1 times
Welcome 2 times
Welcome 3 times
参考
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html