for val in list; do commands done
list参数提供了一些列用于迭代的值,val值依次赋值为list中的值,知道list轮询结束。
$cat test #!/bin/bash # basic for command for test in A B C; do echo the next val is $test done $./test the next val is A the next val is B the next val is C
$cat test #!/bin/bash # using a variable to hold the list list="A B C" list=$list" D" for test in $list; do echo the next val is $test done $./test the next val is A the next val is B the next val is C the next val is D
$cat test #!/bin/bash # reading value from a file file="alphabet" for test in `cat $file`; do echo the next val is $test done $./test the next val is A the next val is B the next val is C
IFS.OLD=$IFS IFS=$‘\n‘ <use the new IFS value in code> IFS=$IFS.OLD
$cat test #!/bin/bash #iterate through all the files in a directory for file in /home/test/* ; do if [ -d "$file" ]; then echo "$file is a directory" elif [ -f "$file" ]; then echo "$file is a file" fi done
原文:http://www.cnblogs.com/hancq/p/5024561.html