for 变量 in 列表 do command1 command2 ... commandN 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显示主目录下以 .bash 开头的文件:
#!/bin/bash for FILE in $HOME/.bash* do echo $FILE done运行结果:
/root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc
原文地址:http://blog.csdn.net/wu20093346/article/details/47210871