第一段代码
str="this is shell";
for i in $str
do
echo $i;
done
输出的结果是:
this
is
shell
第二段代码
for i in "this is shell"
do
echo $i
done
输出的结果是:
this is shell
第一段,用变量,for循环相当于 for i in this is shell 相当于i有3个参数this is shell
第二段用引号,是一个整体,i就只有一个值了
第一个循环是一个引用, 空格会被解释为IFS字符, for in遍历时会在空格处拆分
第二个由于循环体最外层是双引号,空格会被解释为一般的字符,所以不会进行拆分
第二个循环如果用第一个的写法是 for i in "$str"