| 订阅 | 在线投稿
分享
 
 
 

Shell编程基础:单引号和双引号含义区别

来源:互联网  宽屏版  评论
2008-06-01 03:14:44

单引号和双引号都能关闭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#

 
单引号和双引号都能关闭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#
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有