Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)(二)

2014-11-24 02:40:31 · 作者: · 浏览: 5
ome strings
end of string
[root@jfht ~]#

双引号(double quote,partial quoting)
Advanced Bash-Scripting Guide: Chapter 3. Special Characters 写道
partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING.
在双引号中,大部分特殊字符失去了其特殊含义,比如 注释(#)、命令分隔符(;)、管道线(|)、通配符( *)等。

[root@jfht work191]# echo *
cgen ct08 hycu2 hyfc2 jlib keyc mdbc msgc sms web_spider xqt_admin xqt_demo xqt_server zjlt_mhr_files zjlt_mhr_server zjlt_mhr_user zjlt_mhr_web
[root@jfht work191]# echo "*"
*
[root@jfht work191]# echo #

[root@jfht work191]# echo "#"
#
[root@jfht work191]#

Advanced Bash-Scripting Guide: Chapter 5. Quoting 写道
This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape). Keeping $ as a special character within double quotes permits referencing a quoted variable ("$variable"), that is, replacing the variable with its value
man bash 写道
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, and \.

在双引号中,特殊字符包括:美元符($)、反引号(`)、转义符(\)、感叹号(!),其他的特殊字符就不用管了。(为啥在此处写上“感叹号(!)”呢,请看后文分解)

man bash 写道
The characters $ and ` retain their special meaning within double
quotes.

在双引号中,美元符($)可以引用变量,将被替换为变量的值,比如
"$VAR" 被替换为变量VAR的值,但"$VAR123"将被替换为变量VAR123的值,所以最好用大括号把变量括起来,避免意想不到的错误出现;
"${VAR}" 被替换为变量VAR的值,"${VAR}123"将被替换为变量VAR的值再跟上123;
"${VAR:-DEFAULT}" 当变量VAR没有定义或者为空时,替换为DEFAULT,否则替换为变量VAR的值;
"$(command line)" 相当于 "`command line`",将被替换为命令行执行的标准输出信息

变量VAR没有定义。
[root@jfht ~]# echo "$VAR"

[root@jfht ~]# echo "${VAR}"

[root@jfht ~]# echo "$VAR123"

[root@jfht ~]# echo "${VAR}123"
123
[root@jfht ~]# echo "${VAR:-DEFAULT}"
DEFAULT
现在定义VAR变量。
[root@jfht ~]# VAR="Hello World"
[root@jfht ~]# echo "$VAR"
Hello World
[root@jfht ~]# echo "${VAR}"
Hello World
[root@jfht ~]# echo "$VAR123"

[root@jfht ~]# echo "${VAR}123"
Hello World123
[root@jfht ~]# echo "${VAR:-DEFAULT}"
Hello World
现在测试一下命令执行输出结果。
[root@jfht ~]# echo "$(pwd)"
/root
[root@jfht ~]#

在双引号中,反引号(`)将被替换为命令行执行的标准输出信息
"`ls`" 相当于 "$(ls)",将被替换为ls命令的执行结果,即列出当前目录的文件名;
"`date +%F`"相当于"$(date +%F)",将被替换为date +%F的执行结果,即当天的日期,比如2011-09-01

[root@jfht ~]# echo "`pwd`"
/root
[root@jfht ~]# echo "`date +%F`"
2011-09-02
[root@jfht ~]# echo "$(date +%F)"
2011-09-02
[root@jfht ~]#

man bash 写道
The backslash retains its special meaning only when followed
by one of the following characters: $, `, ", \, or . A double
quote may be quoted within double quotes by preceding it with a back-
slash.

在双引号中,转义符(\)是为了方便创建包含特殊字符的字符串,特殊字符的前面需要加上\进行转义
\" 双引号 gives the quote its literal meaning
\$ 美元符 gives the dollar sign its literal meaning (variable name following \$ will not be referenced)
\\ 反斜杠、转义符本身 gives the backslash its literal meaning
\` 反引号
\ 就是\跟上换行,那么换行的作用不再有,只起到续行的作用。

[root@jfht ~]# echo ""

[root@jfht ~]# echo "hello
> world"
hello
world
[root@jfht ~]#
[root@jfht ~]# echo "hello world\""
hello world"
[root@jfht ~]# echo "hello world\$"
hello world$
[root@jfht ~]# echo "hello world\\"
hello world\
[root@jfht ~]# echo "hello world\`"
hello world`
[root@jfht ~]# echo "hello world\
> yes"
hello worldyes
[root@jfht ~]#

在双引号中,感叹号(!)的含义根据使用的场合有所不同,在命令行环境,它将被解释为一个历史命令,而在脚本中,则不会有特殊含义。
Advanced Bash-Scripting Guide: 5.1. Quoting Variables 写道
Encapsulating "!" within double quotes gives an error when used from the command line. This is interpreted as a history command