linux 字符串截取

lishell中截取字符串的方法很多

[
linux 字符串截取
li

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">linux 字符串截取{var##/}
linux 字符串截取{var%%/
}
linux 字符串截取{var:start}
linux 字符串截取{var:0-start}</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")


下面用几个例子展示一下:

1) 获得字符串的长度

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${#var}</pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{#str} echo "length : [${length}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : [61]</pre>


2) 使用 # 和 ## 获取尾部子字符串

2.1) # 最小限度从前面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter#word} </pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str#*/} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]</pre>

2.2) ## 最大限度从前面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter##word}</pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str##*/} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell-truncating-string]</pre>


3) 使用 % 和 %% 获取头部子字符串

3.1) % 最小限度从后面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter%word} </pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str%/*}
echo "substr : [${substr}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://www.fengbohello.xin3e.com/blog]</pre>

3.2) %% 最大限度从后面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter%%word}</pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str%%/*}
echo "substr : [${substr}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]</pre>


4)使用 ${var:} 模式获取子字符串

4.1) 指定从左边第几个字符开始以及子串中字符的个数

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:start:len}</pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str:0:7} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]</pre>

4.2) 从左边第几个字符开始一直到结束

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:7}</pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str:7} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]</pre>

4.3) 从右边第几个字符开始以及字符的个数

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:0-start:len}</pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str:0-23:5} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]</pre>

4.4) 从右边第几个字符开始一直到结束

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:0-start}</pre>

示例代码:

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [linux 字符串截取{str:0-6} echo "substr : [${substr}]"</pre>

[
linux 字符串截取
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]</pre>

上一篇:[LeetCode] Reverse Words in a String II 翻转字符串中的单词之二


下一篇:Linux Shell 截取字符串