变量替换测试
语法 |
说明 |
${变量名#匹配规则} |
从变量开头进行规则匹配,将符合最短的数据删除 |
${变量名##匹配规则} |
从变量开头进行规则匹配,将符合最长的数据删除 |
${变量名%匹配规则} |
从变量尾部进行规则匹配,将符合最短的数据删除 |
${变量名%匹配规则} |
从变量尾部进行规则匹配,将符合最长的数据删除 |
${变量名/旧字符串/新字符串} |
变量内容符合旧字符串则,第一个旧字符串会被新字符串取代 |
${变量名//旧字符串/新字符串} |
变量内容符合旧字符串则,全部的旧字符串会被新字符串取代 |
[master@instance-2m5iiamd ~]$ variable="Linux is very funny! Yes, it is."
[master@instance-2m5iiamd ~]$ echo $variable
Linux is very funny! Yes, it is.
[master@instance-2m5iiamd ~]$ variable1=${variable#*is}
[master@instance-2m5iiamd ~]$ echo $variable
Linux is very funny! Yes, it is.
[master@instance-2m5iiamd ~]$ echo $variable1
very funny! Yes, it is.
[master@instance-2m5iiamd ~]$ variable2=${variable##*is}
[master@instance-2m5iiamd ~]$ echo $variable2
.
[master@instance-2m5iiamd ~]$ variable3=${variable%is}
[master@instance-2m5iiamd ~]$ echo $variable3
Linux is very funny! Yes, it is.
[master@instance-2m5iiamd ~]$ variable3=${variable%is*}
[master@instance-2m5iiamd ~]$ echo $variable3
Linux is very funny! Yes, it
[master@instance-2m5iiamd ~]$ variable4=${variable%%is*}
[master@instance-2m5iiamd ~]$ echo $variable4
Linux
[master@instance-2m5iiamd ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/master/.local/bin:/home/master/bin
[master@instance-2m5iiamd ~]$ variable4=${PATH/bin/BIN}
[master@instance-2m5iiamd ~]$ echo $variable4
/usr/local/BIN:/usr/bin:/usr/local/sbin:/usr/sbin:/home/master/.local/bin:/home/master/bin
[master@instance-2m5iiamd ~]$ variable5=${PATH//bin/BIN}
[master@instance-2m5iiamd ~]$ echo $variable5
/usr/local/BIN:/usr/BIN:/usr/local/sBIN:/usr/sBIN:/home/master/.local/BIN:/home/master/BIN
[master@instance-2m5iiamd ~]$