单引号和双引号都能关闭shell对特殊字符的处理。不同的是,双引号没有单引号严格,单引号关闭所有有特殊作用的字符,而双引号只要求shell忽略大多数,具体的说,就是①美元符号②反引号③反斜杠,这3种特殊字符不被忽略。 不忽略美元符号意味着shell在双引号内部也进行变量名替换。
下面用一个简单的shell程序要说明一下。
debian:~/learn/shell# cat phonebook
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Susan Topple 212-555-4932
Tony Iannino 973-555-1295
Stromboli Pizza 973-555-9478
debian:~/learn/shell#
debian:~/learn/shell# cat lu
# Look someone up in the phone book
grep "$1" phonebook
debian:~/learn/shell#
这是正确的lu程序,下面是运行结果。
debian:~/learn/shell# ./lu 'Susan T'
Susan Topple 212-555-4932
debian:~/learn/shell# ./lu Tony
Tony Iannino 973-555-1295
debian:~/learn/shell#
如果lu写成①grep $1 phonebook或者②grep '$1' phonebook,就会出现下面的错误结果(为什么?)。
①的结果:
debian:~/learn/shell# ./lu Tony //这种情况结果正确
Tony Iannino 973-555-1295
debian:~/learn/shell# ./lu 'Susan T' //这种情况结果错误
grep: T: No such file or directory
phonebook:Susan Goldberg 201-555-7776
phonebook:Susan Topple 212-555-4932
debian:~/learn/shell#
②的结果:
debian:~/learn/shell# ./lu Tony //这种情况结果错误
debian:~/learn/shell# ./lu 'Susan T' //这种情况结果也错误
debian:~/learn/shell#