字符串切片:
${var:offset:length}
[[email protected] ~]#mypath="/etc/sysconfig/network-scripts/" [[email protected] ~]# echo ${mypath:5} #偏移5个字符显示 sysconfig/network-scripts/ [[email protected] ~]# echo ${mypath:10} #偏移10个字符显示 nfig/network-scripts/ [[email protected] ~]# echo ${mypath:5:5} #偏移5个字符,取5个字符 sysco
取出字符串的最后几个字符:${var: -length}
注意:-length之前有空白字符;
[[email protected] ~]# echo ${mypath: -10} k-scripts/
基于模式取子串
${var#*word}:自左而右,查找var变量中存储的字符串中第一次出现的由word所指明的字符,删除此字符及其左侧的所有内容;
[[email protected] ~]#mypath="/etc/sysconfig/network-scripts" [[email protected] ~]# echo ${mypath#*/} etc/sysconfig/network-scripts
${var##*word}:自左而右,查找var变量中存储的字符串中最后一次出现的由word所指明的字符,删除此字符及其左侧的所有内容;
[[email protected] ~]#mypath="/etc/sysconfig/network-scripts" [[email protected] ~]# echo ${mypath##*/} network-scripts
示例:
[[email protected] ~]#mypath="/etc/sysconfig/network-scripts" [[email protected] ~]# echo ${mypath##*c} ripts [[email protected] ~]# echo ${mypath#*c} /sysconfig/network-scripts
${var%word*}:自右而左,查找var变量中存储的字符串中第一次出现的由word所指明的字符,删除此字符及其右侧的所有内容;
${var%%word*}:自右而左,查找var变量中存储的字符串中最后一次出现的由word所指明的字符,删除此字符及其右侧的所有内容;
示例:
[[email protected] ~]#mypath="/etc/sysconfig/network-scripts" [[email protected] ~]# echo ${mypath%c*} /etc/sysconfig/network-s [[email protected] ~]# echo ${mypath%%c*} /et
示例:取一个URL的协议和端口
[[email protected] ~]#url="http://www.baidu.com:80" [[email protected] ~]# echo ${url##*:} 80 [[email protected] ~]# echo ${url%%:*} http
查找替换:
${var/pattern/replacement}:查找var变量存储的字符中第一次由pattern匹配到的内容,并替换为replacement;
[[email protected] ~]#url="http://www.baidu.com:80" [[email protected] ~]# echo ${url/www/WWW} http://WWW.baidu.com:80 [[email protected] ~]# echo ${url/w/W} http://Www.baidu.com:80
${var//pattern/replacement}:查找var变量存储的字符中所有能够由pattern匹配到的内容,并替换为replacement;
[[email protected] ~]# echo ${url//w/W} http://WWW.baidu.com:80
${var/#pattern/replacement}:查找var变量存储的字符中最开始处能够由pattern匹配到的内容,并替换为replacement;
[[email protected] ~]# userinfo="root:x:0:0:rootuser:/root:/bin/bash" [[email protected] ~]# echo ${userinfo/#root/ROOT} ROOT:x:0:0:root user:/root:/bin/bash
${var/%pattern/replacement}:查找var变量存储的字符中最后位置能够由pattern匹配到的内容,并替换为replacement;
[[email protected] ~]# userinfo="root:x:0:0:rootuser:/root:/bin/root" [[email protected] ~]# 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匹配到的内容,并删除;
[[email protected] ~]# userinfo="root:x:0:0:rootuser:/root:/bin/root" [[email protected] ~]# echo ${userinfo/root} :x:0:0:root user:/root:/bin/root [[email protected] ~]# echo ${userinfo//root} :x:0:0: user:/:/bin/ [[email protected] ~]# echo ${userinfo/#root} :x:0:0:root user:/root:/bin/root [[email protected] ~]# echo ${userinfo/%root} root:x:0:0:root user:/root:/bin/
字符串大小写转换:
${var^^}:把var变量中的所有小写字母,统统替换为大写;
${var,,}:把var变量中的所有大写字母,统统替换为小写;
[[email protected] ~]# echo $userinfo root:x:0:0:root user:/root:/bin/root [[email protected] ~]# myinfo=${userinfo^^} [[email protected] ~]# echo $myinfo ROOT:X:0:0:ROOT USER:/ROOT:/BIN/ROOT [[email protected] ~]# echo ${myinfo,,} root:x:0:0:root user:/root:/bin/root
变量赋值:
${var:-word}:如果变量var为空或未声明,则返回word所表示的字符串;否则,则返回var变量的值;
[[email protected] ~]# echo $name #这行就是空的 [[email protected] ~]# echo ${name:-tom} tom [[email protected] ~]# name=hello [[email protected] ~]# echo ${name:-tom} hello
${var:=word}:如果变量var为空或未声明,则返回word所表示的字符串,并且把word赋值为var变量;否则,则返回var变量的值;
[[email protected] ~]# echo $name #这行就是空的 [[email protected] ~]# name=${name:-tom} [[email protected] ~]# echo $name tom [[email protected] ~]# name=${name:-jerry} [[email protected] ~]# echo $name tom
${var:?error}:如果变量var为空或未声明,则返回error为错误信息;否则,则返回var变量的值;
[[email protected] ~]# echo "User‘s name is${name:?wrong}" -bash: name: wrong [[email protected] ~]# name=tom [[email protected] ~]# echo "User‘s name is${name:?wrong}" User‘s name is tom
${var:+word}:如果变量var为空或未声明,忽略;否则,则返回word;
[[email protected] ~]# unset name [[email protected] ~]# echo "User‘s name is${name:+wrong}" User‘s name is [[email protected] ~]# name=tom [[email protected] ~]# echo "User‘s name is${name:+wrong}" User‘s name is wrong