由于blog各种垃圾评论太多,而且本人审核评论周期较长,所以懒得管理评论了,就把评论功能关闭,有问题可以直接qq骚扰我

bash字符串处理

Linux 西门飞冰 7623℃

字符串切片

${var:offset:length}

[root@localhost ~]# mypath="/etc/sysconfig/network-scripts/"
[root@localhost ~]# echo ${mypath:5}   #偏移5个字符显示
sysconfig/network-scripts/
[root@localhost ~]# echo ${mypath:10}  #偏移10个字符显示
nfig/network-scripts/
[root@localhost ~]# echo ${mypath:5:5} #偏移5个字符,取5个字符 
sysco

取出字符串的最后几个字符:${var: -length}

注意:-length之前有空白字符;

[root@localhost ~]# echo ${mypath: -10}
k-scripts/

基于模式取字串

${var#*word}:自左而右,查找var变量中存储的字符串中第一次出现的由word所指明的字符,删除此字符及其左侧的所有内容;

[root@localhost ~]# mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath#*/}
etc/sysconfig/network-scripts

${var##*word}:自左而右,查找var变量中存储的字符串中最后一次出现的由word所指明的字符,删除此字符及其左侧的所有内容;

[root@localhost ~]# mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath##*/}
network-scripts

示例:

[root@localhost ~]# mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath##*c}
ripts
[root@localhost ~]# echo ${mypath#*c}
/sysconfig/network-scripts

${var%word*}:自右而左,查找var变量中存储的字符串中第一次出现的由word所指明的字符,删除此字符及其右侧的所有内容;
${var%%word*}:自右而左,查找var变量中存储的字符串中最后一次出现的由word所指明的字符,删除此字符及其右侧的所有内容;

示例:

[root@localhost ~]# mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath%c*}
/etc/sysconfig/network-s
[root@localhost ~]# echo ${mypath%%c*}
/et

示例:取一个URL的协议和端口

[root@localhost ~]# url="http://www.baidu.com:80"
[root@localhost ~]# echo ${url##*:}
80
[root@localhost ~]# echo ${url%%:*}
http

查找替换

${var/pattern/replacement}:查找var变量存储的字符中第一次由pattern匹配到的内容,并替换为replacement;

[root@localhost ~]# url="http://www.baidu.com:80"
[root@localhost ~]# echo ${url/www/WWW}
http://WWW.baidu.com:80
[root@localhost ~]# echo ${url/w/W}
http://Www.baidu.com:80

${var//pattern/replacement}:查找var变量存储的字符中所有能够由pattern匹配到的内容,并替换为replacement;

[root@localhost ~]# echo ${url//w/W}
http://WWW.baidu.com:80

${var/#pattern/replacement}:查找var变量存储的字符中最开始处能够由pattern匹配到的内容,并替换为replacement;

[root@localhost ~]# userinfo="root:x:0:0:root user:/root:/bin/bash"
[root@localhost ~]# echo ${userinfo/#root/ROOT}
ROOT:x:0:0:root user:/root:/bin/bash

${var/%pattern/replacement}:查找var变量存储的字符中最后位置能够由pattern匹配到的内容,并替换为replacement;

[root@localhost ~]# userinfo="root:x:0:0:root user:/root:/bin/root"
[root@localhost ~]# echo ${userinfo/%root/ROOT}
root:x:0:0:root user:/root:/bin/ROOT

查找删除

${var/pattern}:查找var变量存储的字符中第一次由pattern匹配到的内容,并删除;
${var//pattern}:查找var变量存储的字符中所有能够由pattern匹配到的内容,并删除;
${var/#pattern}:查找var变量存储的字符中最开始处能够由pattern匹配到的内容,并删除;
${var/%pattern}:查找var变量存储的字符中最后位置能够由pattern匹配到的内容,并删除;

[root@localhost ~]# userinfo="root:x:0:0:root user:/root:/bin/root"
[root@localhost ~]# echo ${userinfo/root}
:x:0:0:root user:/root:/bin/root
[root@localhost ~]# echo ${userinfo//root}
:x:0:0: user:/:/bin/
[root@localhost ~]# echo ${userinfo/#root}
:x:0:0:root user:/root:/bin/root
[root@localhost ~]# echo ${userinfo/%root}
root:x:0:0:root user:/root:/bin/

字符串大小写转换

${var^^}:把var变量中的所有小写字母,统统替换为大写;
${var,,}:把var变量中的所有大写字母,统统替换为小写;

[root@localhost ~]# echo $userinfo
root:x:0:0:root user:/root:/bin/root
[root@localhost ~]# myinfo=${userinfo^^}
[root@localhost ~]# echo $myinfo
ROOT:X:0:0:ROOT USER:/ROOT:/BIN/ROOT
[root@localhost ~]# echo ${myinfo,,}
root:x:0:0:root user:/root:/bin/root

变量赋值

${var:-word}:如果变量var为空或未声明,则返回word所表示的字符串;否则,则返回var变量的值;

[root@localhost ~]# echo $name

[root@localhost ~]# echo ${name:-tom}
tom
[root@localhost ~]# name=hello 
[root@localhost ~]# echo ${name:-tom}
hello

${var:=word}:如果变量var为空或未声明,则返回word所表示的字符串,并且把word赋值为var变量;否则,则返回var变量的值;

[root@localhost ~]# echo $name

[root@localhost ~]# name=${name:-tom}
[root@localhost ~]# echo $name
tom
[root@localhost ~]# name=${name:-jerry}
[root@localhost ~]# echo $name
tom

${var:?error}:如果变量var为空或未声明,则返回error为错误信息;否则,则返回var变量的值;

[root@localhost ~]# echo "User's name is ${name:?wrong}"
-bash: name: wrong
[root@localhost ~]# name=tom
[root@localhost ~]# echo "User's name is ${name:?wrong}"
User's name is tom

${var:+word}:如果变量var为空或未声明,忽略;否则,则返回word;

[root@localhost ~]# unset name
[root@localhost ~]# echo "User's name is ${name:+wrong}"
User's name is 
[root@localhost ~]# name=tom
[root@localhost ~]# echo "User's name is ${name:+wrong}"
User's name is wrong

转载请注明:西门飞冰的博客 » bash字符串处理

喜欢 (1)or分享 (0)