首先需要明白的一点是我们通常所用的变量替代 $varname 其实是 ${varname}的一种简写形式。
其次,需要知道字符串操作的核心思想是:Bash shell字符串操作是通过在变量名‘varname‘和闭花括号‘}‘之间插入操作符来完成的。
字符串操作符在处理" ‘ ? * 等字符时需要使用\转意
grep 对字符进行查找时 " 需要加转意\ ‘不需要加
替换操作符
${varname:-word}
含义:如果变量varname存在且非空, 返回变量的值;否则返回值word。
目的:如果变量未定义,则返回默认值word。
举例:如果变量count未定义,则:${count:-0}的值为0.
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $count
[email protected]/export/home/ftp/pub-->echo ${count:-0}
0
[email protected]/export/home/ftp/pub-->echo $count
[email protected]/export/home/ftp/pub-->
${varname:=word}
含义:如果变量varname存在且非空,返回变量的值;否则将变量的值设为值word后返回变量的值。
目的:如果变量varname未定义,则将其设置为默认值word.
举例:如果变量count未定义,则将其设为0
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $count
[email protected]/export/home/ftp/pub-->echo ${count:=0}
0
[email protected]/export/home/ftp/pub-->echo $count
0
${varname:+word}
含义:如果变量varname存在且非空,返回值word;否则返回null。
目的:测试变量varname是否存在。
举例:如果变量count存在,返回1(表示存在)
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $count
0
[email protected]/export/home/ftp/pub-->echo ${count:+1}
1
[email protected]/export/home/ftp/pub-->unset count
[email protected]/export/home/ftp/pub-->echo $count
[email protected]/export/home/ftp/pub-->echo ${count:+1}
[email protected]/export/home/ftp/pub-->
${varname:?message}
含义:如果变量varname存在且非空,返回变量的值;否则打印信息"varname: message";如果message没有指定,则显示parameter null or not set。
目的:如果变量未定义,则打印错误信息。
举例:如果变量count未定义,打印消息"count: undefined!
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $count
[email protected]/export/home/ftp/pub-->echo ${count:?"undefined"}
bash: count: undefined
${varname:offset:length}
含义:返回变量varname的子字符串,开始位置为offset,长度为length。注意索引开始位置为0。
目的:返回子字符串。
举例:如果变量count的值为 frogfootman,则:
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->count=frogfootman
[email protected]/export/home/ftp/pub-->echo ${count:4}
footman
[email protected]/export/home/ftp/pub-->echo ${count:4:4}
foot
[email protected]/export/home/ftp/pub-->
${varname/pattern/string}
含义:用值string替换varname中模式pattern可以匹配到的最大部分。仅替换第一次匹配。
举例:
[c-sharp] view plaincopy
[email protected]/-->echo $PATH
/usr/sbin:/usr/bin:/export/home/ftp/pub/:/etc/vx/bin:/opt/VRTS/bin:/opt/VRTSvcs/bin:/opt/VRTSat/bin:/sbin:/usr/sbin:/usr/local/bin:/export/home/ftp/pub/
[email protected]/-->echo -e ${PATH/:/‘/n‘}
/usr/sbin
/usr/bin:/export/home/ftp/pub/:/etc/vx/bin:/opt/VRTS/bin:/opt/VRTSvcs/bin:/opt/VRTSat/bin:/sbin:/usr/sbin:/usr/local/bin:/export/home/ftp/pub/
[email protected]/-->
${varname//pattern/string}
含义:用值string替换varname中模式pattern可以匹配到的最大部分。全局匹配。
举例:
[c-sharp] view plaincopy
[email protected]/-->echo $PATH
/usr/sbin:/usr/bin:/export/home/ftp/pub/:/etc/vx/bin:/opt/VRTS/bin:/opt/VRTSvcs/bin:/opt/VRTSat/bin:/sbin:/usr/sbin:/usr/local/bin:/export/home/ftp/pub/
[email protected]/-->echo -e ${PATH//:/‘/n‘}
/usr/sbin
/usr/bin
/export/home/ftp/pub/
/etc/vx/bin
/opt/VRTS/bin
/opt/VRTSvcs/bin
/opt/VRTSat/bin
/sbin
/usr/sbin
/usr/local/bin
/export/home/ftp/pub/
变量长度
${#varname}
含义:获取变量varname值长度。
举例:
[c-sharp] view plaincopy
[email protected]/-->echo $path
/export/home/ftp/pub
[email protected]/-->echo ${#path}
20
扩展模式匹配操作府
注意:使用扩展模式匹配操作符时,shopt extglob必须为on .
[c-sharp] view plaincopy
[email protected]/tmp/ewang-->shopt extglob
extglob on
*(patternlist)
含义:匹配patternlist中0个或多个pattern
举例:
[c-sharp] view plaincopy
[email protected]/-->var=alicealicesophie
[email protected]/-->echo ${var/*(alice|edward|stone)/ALICE}
ALICEsophie
+(patternlist)
含义:匹配patternlist中1个或多个pattern
举例:
[c-sharp] view plaincopy
[email protected]/-->var=alicesophiealicestone
[email protected]/-->echo ${var/+(alice|edward|stone)/ALICE}
ALICEsophiealicestone
[email protected]/-->echo ${var//+(alice|edward|stone)/ALICE}
ALICEsophieALICE
?(patternlist)
含义:匹配patternlist中0各或1个patter
@(patternlist)
含义:匹配patterlist中的1个pattern.
举例:
[c-sharp] view plaincopy
[email protected]/-->count=123.5
[email protected]/-->echo ${count/@(.)/#}
123#5
!(patternlist)
含义:匹配patterlist中所有pattern之外的pattern。
举例:
[c-sharp] view plaincopy
[email protected]/tmp/ewang-->ls
abc mn tv1 vt0 vt1 vt2 vt3
[email protected]/tmp/ewang-->rm -fr !(vt+([0-9]))
[email protected]/tmp/ewang-->ls
vt0 vt1 vt2 vt3
[email protected]/tmp/ewang-->
模式匹配操作符
${varname#pattern}
含义:如果pattern匹配变量varname值的开始部分,删除最小 的匹配部分并且返回变量剩余部分内容。
举例:
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $path
/export/home/ftp/pub
[email protected]/export/home/ftp/pub-->echo ${path#*/}
export/home/ftp/pub
${varname##pattern}
含义:如果pattern匹配变量varname值的开始部分,删除最大 的匹配部分并且返回变量剩余部分内容。
举例:
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $path
/export/home/ftp/pub
[email protected]/export/home/ftp/pub-->echo ${path##*/}
pub
${varname%pattern}
含义:如果pattern匹配变量varname值的结尾部分,删除最小 的匹配部分并且返回变量剩余部分内容。
举例:
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $path
/export/home/ftp/pub
[email protected]/export/home/ftp/pub-->echo ${path%/*}
/export/home/ftp
${varname%%pattern}
含义:如果pattern匹配变量varname值的结尾部分,删除最大 的匹配部分并且返回变量剩余部分内容。
举例:
[c-sharp] view plaincopy
[email protected]/export/home/ftp/pub-->echo $file
alicece
[email protected]/export/home/ftp/pub-->echo ${file%ce*}
alice
[email protected]/export/home/ftp/pub-->echo ${file%%ce*}
ali