shell--字符串的截取变量子串串
1.${par}返回变量的内容
[root@test ~]# export par=cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325
2. ${#par}返回变量par的长度
[root@test ~]# export par=cdc7c8e4-45c6-420b-8574-02fae43b7325
[root@test ~]# echo ${#par} 36
3. ${par:nu}在变量中从nu之后提取到文件尾
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par:10} 5c6-420b-8574-02fae43b7325
4. ${par:nu:num}从变量中从nu之后提取nmu个字符
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par:10:3} 5c6
5. ${par#fa}删除变量中匹配字符的最小范围(从变量头开始)
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par#c*e} 4-45c6-420b-8574-02fae43b7325
6. ${par##fa}删除变量中匹配字符的最大范围(从变量头开始)
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par##c*e} 43b7325
7. ${par%fa}提取变量中匹配字符的最小范围(从变量尾开始)
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par%0*5} cdc7c8e4-45c6-420b-8574-
8. ${par%%fa}提取变量中匹配字符的最大范围(从变量尾开始)
cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par%%0*5} cdc7c8e4-45c6-42
9. ${par/ss/gg}在par变量中使用gg替换ss(仅替换一次)
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par/cdc/abc} abc7c8e4-45c6-420b-8574-02fae43b7325
10. ${par/ss/gg}在par变量中使用gg替换ss(全部替换)
[root@test ~]# echo ${par} cdc7c8e4-45c6-420b-8574-02fae43b7325 [root@test ~]# echo ${par//c/C} CdC7C8e4-45C6-420b-8574-02fae43b7325
11. ${par:-no}若par变量为空则使用no值代替
[root@test ~]# echo ${par} [root@test ~]# echo ${par:-no} no
12. ${par:=no}若par变量为空则赋值为no
[root@test ~]# echo ${par} [root@test ~]# echo ${par:=no} no [root@test ~]# echo $par no
13 ${par:?no}若par值为空则显示错误信息
[root@test ~]# echo ${par} [root@test ~]# echo ${par:?no} -bash: par: no
[root@test ~]# par=a
[root@test ~]# echo ${par:?no}
a
14.${par:+no}若par值为空则不做任何操作,若包含值则使用no替换
[root@test ~]# echo ${par} [root@test ~]# par=a [root@test ~]# echo ${par:+no} no