1. for命令
下面是bash shell中for命令的基本格式。
for var in list do
commands
done
每次for命令遍历值列表,它都会将列表中的下个值赋给$test
变量。$test
变量可以像for 命令语句中的其他脚本变量一样使用。在最后一次迭代后,$test
变量的值会在shell脚本的剩余 部分一直保持有效。它会一直保持最后一次迭代的值(除非你修改了它)。
$ cat for_test.sh
#!/bin/bash
for test in Alabama Alaska Arizona Arkansas California
do
echo The text next state is $test
done
echo "The last state we visited was $test"
test=hello
echo "Wait, now we're visiting $test"
$ bash for_test.sh
The text next state is Alabama
The text next state is Alaska
The text next state is Arizona
The text next state is Arkansas
The text next state is California
The last state we visited was California
Wait, now we're visiting hello
从变量中读取值
#!/bin/bash 13
# using a variable to hold the list
l