1、标准方式:
for var in list
do
commands
done
2、一行书写方式:
for var in list; do
3、读取列表中的值
for test in Nevada New Hampshire New Mexico New York North Carolina
do
echo "Now going to $test"
done
4、从变量读取列表
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state?"
done
$ ./test4
Have you ever visited Alabama?
Have you ever visited Alaska?
5、从命令读取值
file="states"
for state in $(cat $file)
do
echo "Visit beautiful $state"
done
$ cat states
Alabama
Alaska
Arizona
$ ./test5
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
6、在bash shell C 语言的 for 命令
for (( i=1; i <= 10; i++ ))
do
echo "The next number is $i"
done
The next number is 1
The next number is 2
The next number is 3
7、bash shell C 语言的 for 命令,使用多个变量
for (( a=1, b=10; a <= 10; a++, b-- ))
do
echo "$a - $b"
done
1 - 10
2 - 9
3 - 8