`

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

阅读更多

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

In Java

你懂的!

使用单引号表示字符常量:'c'

使用双引号表示字符串常量:"hello world"

 

Java字符转义

\u???? 四位十进制Unicode字符

\??? 三位八进制字符

\n 换行(\u000a)(Insert a newline in the text at this point.)

\t 水平制表符(\u0009)(Insert a tab in the text at this point.)

\b 退格(\u0008)(Insert a backspace in the text at this point.)

\r 回车(\u000d)(Insert a carriage return in the text at this point.)

\f 换页(\u000c)(Insert a formfeed in the text at this point.)

\' 单引号(\u0027)(Insert a single quote character in the text at this point.)

\" 双引号(\u0022)(Insert a double quote character in the text at this point.)

\\ 反斜杠(\u005c)(Insert a backslash character in the text at this point.)

 

注:网上有很多资料都不一定正确,还是看Oracle网站上关于Characters的内容为好:

http://download.oracle.com/javase/tutorial/java/data/characters.html

 

In Bash

字符串的用途

在Bash中无处不是字符串,它可以

(1)作为命令;

(2)作为命令行参数;

(3)作为变量的值;

(4)作为重定向的文件名称;

(5)作为输入字符串(here string, here document);

(6)作为命令行的输出。

 

字符串表示方式概述

在Bash中,字符串常量有多种表示方式

(1)不使用任何引号,比如 hello

(2)使用一对单引号括起来,比如 'hello'

(3)使用一对双引号括起来,比如 "hello"

(4)使用美元符加上一对单引号括起来,比如 $'hello'

如果字符串中不包含特殊字符,那么上述几种表示方式是等效的。

 

友情提示:字符串的这几种表示方式可以任意组合,比如 h'e'"l"$'lo' 与 hello是等效的。

 

[root@jfht ~]# echo hello
hello
[root@jfht ~]# echo 'hello'
hello
[root@jfht ~]# echo "hello"
hello
[root@jfht ~]# echo $'hello'
hello
[root@jfht ~]# echo h'e'"l"$'lo'
hello
[root@jfht ~]#

 

关于字符串表示的问题

在进一步对字符串的表示方式了解之前,请先回答下面的问题:

问题1:字符串中包含空格,应该怎么表示?

问题2:字符串中包含制表符,应该怎么表示?

问题3:字符串中包含单引号,应该怎么表示?

问题4:字符串中包含双引号,应该怎么表示?

问题5:字符串中包含$,应该怎么表示?

问题6:字符串中包含#,应该怎么表示?

问题7:字符串中包含换行,即有多行,应该怎么表示?

问题8:字符串中包含反斜杠,应该怎么表示?

问题9:字符串中包含感叹号,应该怎么表示?

 

无引号(one of quoting  mechanisms:  the  escape  character)

在没有用引号括起来时,所有的特殊字符都会产生特殊的作用,如果想让它们失去特殊作用,就需要用转义符(\)进行转义。

man bash 写道
A non-quoted backslash (\) is the escape character. It preserves the
literal value of the next character that follows, with the exception of
<newline>. If a \<newline> pair appears, and the backslash is not
itself quoted, the \<newline> is treated as a line continuation (that
is, it is removed from the input stream and effectively ignored).

 

空格是用来分隔命令与参数、参数与参数、变量赋值和命令的,所以如果字符串中有空格时,需要对空格进行转义。

[root@jfht ~]# STR=hello world
-bash: world: command not found
[root@jfht ~]# STR=hello\ world

 

换行符是命令行之间的分隔符的一种(另外一种是分号),如果字符串比较长,就可以对换行符进行转义,变成续行。

[root@jfht ~]# STR=hello\
> world
[root@jfht ~]#

 

在没有任何引号括起来时,\跟上字符,那么所跟的字符将失去特殊含义。比如 管道线(|)。

[root@jfht ~]# STR=hello|world
-bash: world: command not found
[root@jfht ~]# STR=hello\|world
[root@jfht ~]#

 

单引号(single quote,full quoting)

Advanced Bash-Scripting Guide: Chapter 3. Special Characters 写道
full quoting [single quote]. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than "STRING".

 

在单引号中,所有的特殊字符豆浆失去其特殊含义。这样也就导致在单引号中无法表示单引号字符本身。

man bash QUOTING 写道
Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur between
single quotes, even when preceded by a backslash.
 

 

[root@jfht ~]# echo '$'
$
[root@jfht ~]# echo '`'
`
[root@jfht ~]# echo '\'
\
[root@jfht ~]# echo '!'
!
[root@jfht ~]# echo 'begin of string
> some strings
> end of string'
begin of string
some strings
end of string
[root@jfht ~]#

 

下面展示一下单引号表示的字符串在脚本中的表现。

 

Bash脚本:test_single_quote.sh

#!/bin/sh

echo '$'

echo '`'

echo '\'

echo '!'

echo 'start of string
some strings
end of string'
 

[root@jfht ~]# chmod +x ./test_single_quote.sh
[root@jfht ~]# ./test_single_quote.sh
$
`
\
!
start of string
some 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 <newline>. 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

\` 反引号

\<newline> 就是\跟上换行,那么换行的作用不再有,只起到续行的作用。

 

[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. Within a script, though, this problem does not occur, since the Bash history mechanism is disabled then.

 

在命令行环境,感叹号(!)称之为“历史扩展字符(the  history  expansion character)”。

[root@jfht ~]# pwd
/root
[root@jfht ~]# echo "!"
-bash: !: event not found
[root@jfht ~]# echo "!pwd"
echo "pwd"
pwd
[root@jfht ~]#

 

在脚本中使用感叹号,将不会进行历史扩展。

 

Bash脚本:test_exclamation_mark.sh

#!/bin/sh

echo "!"

pwd
echo "!pwd"
 

[root@smsgw root]# ./test_exclamation_mark.sh
!
/root
!pwd
[root@smsgw root]#

 

 

$' ... '(ANSI-C Quoting)

前面讲到,包围在单引号之内的字符都不会有特殊含义,所以单引号本身并不能在一对单引号中出现。但是在前面加上$之后,就可以使用\进行转义了,\的转义含义与C语言中的相同。

Advanced Bash-Scripting Guide: Chapter 3. Special Characters 写道
$' ... '
    Quoted string expansion. This construct expands single or multiple escaped octal or hex values into ASCII or Unicode characters.
 
Advanced Bash-Scripting Guide: 5.2. Escaping 写道
The $' ... ' quoted string-expansion construct is a mechanism that uses escaped octal or hex values to assign ASCII characters to variables, e.g., quote=$'\042'.

 

man bash 写道
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped charac-
ters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as fol-
lows:
\a alert (bell)
\b backspace
\e an escape character (not ANSI C)
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

 

叮当一声。

[root@jfht ~]# echo $'\a'

 

叮当三声。
[root@jfht ~]# echo $'\a\a\a'

 

单引号。

[root@jfht ~]# echo $'\''
'

八进制表示的ASCII字符。

[root@jfht ~]# echo $'\101\102\103\010'
ABC
[root@jfht ~]#
[root@jfht ~]#


$"..."(Locale-Specific Translation)

在双引号之前加上$,这部分内容还未能很好的理解,就不说了。望高人指点一二。

man bash 写道
A double-quoted string preceded by a dollar sign ($) will cause the
string to be translated according to the current locale. If the cur-
rent locale is C or POSIX, the dollar sign is ignored. If the string
is translated and replaced, the replacement is double-quoted.

 

 

问题解答

问题1:字符串中包含空格,应该怎么表示?

问题2:字符串中包含制表符,应该怎么表示?

问题3:字符串中包含单引号,应该怎么表示?

问题4:字符串中包含双引号,应该怎么表示?

问题5:字符串中包含$,应该怎么表示?

问题6:字符串中包含#,应该怎么表示?

问题7:字符串中包含换行,即有多行,应该怎么表示?

问题8:字符串中包含反斜杠,应该怎么表示?

问题9:字符串中包含感叹号,应该怎么表示?

 

感谢大家怀着极大的耐心看完了上述内容,上面9道题的答案在不久之后将公布,有兴趣的请直接跟帖回复。

 

 

本文链接:http://codingstandards.iteye.com/blog/1166282   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 1.(字符串)变量声明

下节内容:Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值

 

 

6
4
分享到:
评论
1 楼 xueqiang 2011-09-06  

相关推荐

Global site tag (gtag.js) - Google Analytics