号和双引号)可以防止通配符的扩展:
bi
sh
echo“jpg”echo‘jpg’
其运行结果为:
jpgjpg
其中单引号更严格一些,它可以防止任何变量扩展;而双引号可以防止通配符扩展但允许变量扩展:
bi
sh
echoSHELLecho“SHELL”echo‘SHELL’
运行结果为:
bi
bashbi
bashSHELL
此外还有一种防止这种扩展的方法,即使用转义字符反斜杆:
echojpgechoSHELL
输出结果为:
jpgSHELL
HereDocume
t
当要将几行文字传递给一个命令时,用heredocume
ts是一种不错的方法。对每个脚本写一段帮助性的文字是很有用的,此时如果使用heredocume
ts就不必用echo函数一行行输出。Heredocume
t以开头,后面接上一个字符串,这个字符串还必须出现在heredocume
t的末尾。下面是一个例子,在该例子中,我们对多个文件进行重命名,并且使用heredocume
ts打印帮助:
fbi
sh
wehavelesstha
3argume
tsPri
tthehelptextiflt3the
catHELP
re
re
amesa
umberoffilesusi
gsedregularexpressio
sUSAGEre
‘regexp’‘replaceme
t’files…
EXAMPLEre
ameallHTMfilesi
htmlre
‘HTM’‘html’HTM
HELPexit0fiOLD”1″NEW”2″Theshiftcomma
dremoveso
eargume
tfromthelistofcomma
dli
eargume
tsshiftshiftco
tai
s
owallthefilesforfilei
doiff