格式 | 說明 |
---|---|
${string: start :length} | 從 string 字符串的左邊第 start 個字符開始,向右截取 length 個字符。 |
${string: start} | 從 string 字符串的左邊第 start 個字符開始截取,直到最后。 |
${string: 0-start :length} | 從 string 字符串的右邊第 start 個字符開始,向右截取 length 個字符。 |
${string: 0-start} | 從 string 字符串的右邊第 start 個字符開始截取,直到最后。 |
${string#*chars} | 從 string 字符串第一次出現(xiàn) *chars 的位置開始,截取 *chars 右邊的所有字符。 |
${string##*chars} | 從 string 字符串最后一次出現(xiàn) *chars 的位置開始,截取 *chars 右邊的所有字符。 |
${string%*chars} | 從 string 字符串第一次出現(xiàn) *chars 的位置開始,截取 *chars 左邊的所有字符。 |
${string%%*chars} | 從 string 字符串最后一次出現(xiàn) *chars 的位置開始,截取 *chars 左邊的所有字符。 |
--------------------------------------------------------------------------------
下面用幾個例子展示一下:
1) 獲得字符串的長度
語法:
${#var}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" length=${#str} echo "length : [${length}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] length : [61]
--------------------------------------------------------------------------------
2) 使用 # 和 ## 獲取尾部子字符串
2.1) # 最小限度從前面截取word
語法:
${parameter#word}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #分割符為'/' substr=${str#*/} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]
2.2) ## 最大限度從前面截取word
語法:
${parameter##word}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #分割符為'/' substr=${str##*/} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [shell-truncating-string]
--------------------------------------------------------------------------------
3) 使用 % 和 %% 獲取頭部子字符串
3.1) % 最小限度從后面截取word
語法:
${parameter%word}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" substr=${str%/*} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http://www.fengbohello.xin3e.com/blog]
3.2) %% 最大限度從后面截取word
語法:
${parameter%%word}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" substr=${str%%/*} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http:]
--------------------------------------------------------------------------------
4)使用 ${var:} 模式獲取子字符串
4.1) 指定從左邊第幾個字符開始以及子串中字符的個數(shù)
語法:
${var:start:len}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0 表示左邊第一個字符開始,7 表示子字符的總個數(shù)。 substr=${str:0:7} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http://]
4.2) 從左邊第幾個字符開始一直到結(jié)束
語法:
${var:7}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 7 表示左邊第8個字符開始 substr=${str:7} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]
4.3) 從右邊第幾個字符開始以及字符的個數(shù)
語法:
${var:0-start:len}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0-23 表示右邊算起第23個字符開始,5 表示字符的個數(shù) substr=${str:0-23:5} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [shell]
4.4) 從右邊第幾個字符開始一直到結(jié)束
語法:
${var:0-start}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0-6 表示右邊算起第6個字符開始 substr=${str:0-6} echo "substr : [${substr}]"
執(zhí)行結(jié)果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [string]
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。