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

2014-11-24 02:40:31 · 作者: · 浏览: 7

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
. If a \ pair appears, and the backslash is not
itself quoted, the \ 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
Bash代码
#!/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
s