轉載自:https://www.cnblogs.com/fengbohello/p/5954895.html (作者:郝峰波 Linux Shell 截取字符串)
下文原創作者郝峰波,內容以及足夠詳細,我平常也經常參考,在此直接引用。
shell中截取字符串的方法很多
${var#*/}
${var##*/}
${var%/*}
${var%%/*}
${var:start:len}
${var:start}
${var:0-start:len}
${var:0-start}
下面用幾個例子展示一下:
1) 獲得字符串的長度
語法:
${#var}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"
length=${#str}
echo "length : [${length}]"
執行結果:
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}]"
執行結果:
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}]"
執行結果:
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}]"
執行結果:
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}]"
執行結果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http:]
4)使用 ${var:} 模式獲取子字符串
4.1) 指定從左邊第幾個字符開始以及子串中字符的個數
語法:
${var:start:len}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"
#其中的 0 表示左邊第一個字符開始,7 表示子字符的總個數。
substr=${str:0:7}
echo "substr : [${substr}]"
執行結果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http://]
4.2) 從左邊第幾個字符開始一直到結束
語法:
${var:7}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"
#其中的 7 表示左邊第8個字符開始
substr=${str:7}
echo "substr : [${substr}]"
執行結果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]
4.3) 從右邊第幾個字符開始以及字符的個數
語法:
${var:0-start:len}
示例代碼:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"
#其中的 0-23 表示右邊算起第23個字符開始,5 表示字符的個數
substr=${str:0-23:5}
echo "substr : [${substr}]"
執行結果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [shell]
4.4) 從右邊第幾個字符開始一直到結束
語法:
${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}]"
執行結果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [string]

