shell中的字符串

字符串的声明

shell中的字符串可以用单引号,也可以用双引号,也可以不用引号

单引号

1
str='this is a string'

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的

  • 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。

双引号

1
str="this is a string"

双引号的优点:

  • 双引号里可以有变量

  • 双引号里可以出现转义字符

获取字符串的长度

1
2
string="abcd"
echo ${#string}

提取子字符串

语法 说明
${string:position} 从position位置开始
${string:position:length} 从position位置开始,匹配长度为length
${string:-position} 从右边开始匹配
${string:(position)} 从左边开始匹配
expr substr $string $position $length 从position位置开始,匹配长度为length

以下脚本将输出string 从第12位开始,5位长度的内容

1
2
string="Stopping by Woods on a Snowy Evening, Whose woods these are I think I know."
echo %{string:12:5}

查找字符串

查找字符i或o的位置(哪个字母先出现就计算哪个):

1
2
string="Stopping by Woods on a Snowy Evening, Whose woods these are I think I know."
echo `expr index "$string" io`

注意: 以上脚本 `是反引号

字符串的替换

语法 说明
${变量名#匹配规则} 从变量开头进行规则匹配,将符合最短的数据删除
${变量名##匹配规则 从变量开头进行规则匹配,将符合最长的数据删除
${变量名%匹配规则} 从变量尾部进行规则匹配,将符合最短的数据删除
${变量名%%匹配规则} 从变量尾部进行规则匹配,将符合最长的数据删除
${变量名/旧字符串/新字符串} 变量内容符合旧字符串,则第一个旧字符串会被新字符串取代
${变量名//旧字符串/新字符串} 变量内容符合旧字符串,则全部的旧字符串会被新字符串取代

字符串的测试

变量测试

变量测试表达式可以在变量为空或没有配置时返回特定的表达式,具体操作如下:

变量配置方式 str没有配置 Str为空字符串 Str已配置且非空
var=${str-expr} var=expr var= var=$str
var=${str=expr} var=expr var= var=$str
var=${str:-expr} var=expr var=expr var=$str
var=${str:=expr var=expr var=expr var=$str
var=${str+expr} var= var=expr var=expr
var=${str:+expr} var= var= var=expr

表达式的命令替换

表达式如 expr 会将结果输出到控制台,如果希望作为值,需要使用命令替换语句。语法为:

1
2
`expr`
$(expr)

测试脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# !bin/bash
# 单引号创建字符串
str='Two roads diverged in a yellow wood,'
echo $str
# 双引号创建字符串
str="And sorry I could not travel both"
echo $str
# 双引号拼接字符串
var_and="And"
str1=$var_and" sorry I could not travel both"
str2="$var_and be one traveler, long I stood"
echo $str1 $str2
# 单引号拼接字符串
str1=$var_and' looked down one as far as I could'
str2='$var_and To where it bent in the undergrowth'
echo $str1 $str2
# 获取字符串的长度
echo ${#str1}
# 提取子字符串
echo ${str1:11:4}
# 查找子字符串
echo `expr index "$str1" Aok`
# 字符串的替换
str='Two roads diverged in a yellow wood,'
echo ${str#*o}
echo ${str##*o}
echo ${str%o*}
echo ${str%%o*}
echo ${str/w/f}
echo ${str//w/f}
# 变量的测试
str_empty=""
echo '-'
echo ${str_null-null}
echo ${str_empty-null}
echo ${str-null}
echo '='
echo ${str_null=null}
echo ${str_empty=null}
echo ${str=null}
echo ':-'
echo ${str_null:-null}
echo ${str_empty:-null}
echo ${str:-null}
echo ':='
echo ${str_null:=null}
echo ${str_empty:=null}
echo ${str:=null}
echo '+'
echo ${str_null+null}
echo ${str_empty+null}
echo ${str+null}
echo ':+'
echo ${str_null:+null}
echo ${str_empty:+null}
echo ${str:+null}
# 命令替换
indexOfA=`expr index "$str1" A`
indexOfo=$(expr index "$str1" o)
echo $indexOfA
echo $indexOfo

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Two roads diverged in a yellow wood,
And sorry I could not travel both
And sorry I could not travel both And be one traveler, long I stood
And looked down one as far as I could $var_and To where it bent in the undergrowth
37
down
1
roads diverged in a yellow wood,
d,
Two roads diverged in a yellow wo
Tw
Tfo roads diverged in a yellow wood,
Tfo roads diverged in a yellof food,
-
null

Two roads diverged in a yellow wood,
=
null

Two roads diverged in a yellow wood,
:-
null
null
Two roads diverged in a yellow wood,
:=
null
null
Two roads diverged in a yellow wood,
+
null
null
null
:+
null
null
null
1
6