List="one two three"for a in $List # 空白符将变量分成几个部分。doecho"$a"done# one# two# threeecho"---"for a in"$List"# 在单一变量中保留所有空格。do# ^ ^echo"$a"done# one two three
下面是一个更加复杂的例子:
variable1="a variable containing five words"COMMANDThisis $variable1 # 带上7个参数执行COMMAND命令:# "This" "is" "a" "variable" "containing" "five" "words"COMMAND"This is $variable1"# 带上1个参数执行COMMAND命令:# "This is a variable containing five words"variable2=""# 空值。COMMAND $variable2 $variable2 $variable2# 不带参数执行COMMAND命令。COMMAND"$variable2""$variable2""$variable2"# 带上3个参数执行COMMAND命令。COMMAND"$variable2 $variable2 $variable2"# 带上1个参数执行COMMAND命令(2空格)。# 感谢 Stéphane Chazelas。
echo"Why can't I write 's between single quotes"echo# 可以采取迂回的方式。echo'Why can'\''t I write '"'"'s between single quotes'# |-------| |----------| |-----------------------|# 由三个单引号引用的字符串,再加上转义以及双引号包住的单引号组成。# 感谢 Stéphane Chazelas 提供的例子。