变量替换测试
变量替换测试
语法 | 说明 |
---|---|
$ | 从变量开头进行规则匹配,将符合最短的数据删除 |
$ | 从变量开头进行规则匹配,将符合最长的数据删除 |
$ | 从变量尾部进行规则匹配,将符合最短的数据删除 |
$ | 从变量尾部进行规则匹配,将符合最长的数据删除 |
$ | 变量内容符合旧字符串则,第一个旧字符串会被新字符串取代 |
$ | 变量内容符合旧字符串则,全部的旧字符串会被新字符串取代 |
[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 ~]$
作者: 咕咚!
出处: https://www.cnblogs.com/linga/
关于作者:专注虚拟化,运维开发,RPA,Rust,Go,Python!
本文版权归作者和博客园共有,禁止*.csdn.net转载,禁止以盈利为目的的转载,转载文章,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(oldsixa@163.com)咨询.