${var:offset:length}bash
[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}ide
注意:-length以前有空白字符;url
[root@localhost ~]# echo ${mypath: -10} k-scripts/
${var#*word}:自左而右,查找var變量中存儲的字符串中第一次出現的由word所指明的字符,刪除此字符及其左側的全部內容;spa
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts" [root@localhost ~]# echo ${mypath#*/} etc/sysconfig/network-scripts
${var##*word}:自左而右,查找var變量中存儲的字符串中最後一次出現的由word所指明的字符,刪除此字符及其左側的全部內容;ip
[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所指明的字符,刪除此字符及其右側的全部內容;it
${var%%word*}:自右而左,查找var變量中存儲的字符串中最後一次出現的由word所指明的字符,刪除此字符及其右側的全部內容;class
示例:變量
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts" [root@localhost ~]# echo ${mypath%c*} /etc/sysconfig/network-s [root@localhost ~]# echo ${mypath%%c*} /et
示例:取一個URL的協議和端口network
[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:rootuser:/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:rootuser:/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:rootuser:/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