(四)变量内容的删除替换替代

(1)变量的删除

[root@localhost scripts]# url=www.baidu.com.cn
[root@localhost scripts]# echo ${#url}     获取变量的长度
16
[root@localhost scripts]# echo ${url}       标准查看
www.baidu.com.cn
[root@localhost scripts]# echo ${url#*.}    从前往后,最短匹配
baidu.com.cn
[root@localhost scripts]# echo ${url##*.}   从前往后,最长匹配,贪婪匹配
cn
[root@localhost scripts]# url=www.baidu.com.cn
[root@localhost scripts]# echo ${url%.*}        从后往前,最短匹配
www.baidu.com
[root@localhost scripts]# echo ${url%%.*}       从后往前,最长匹配,贪婪匹配
www

(2)变量索引切片

[root@localhost scripts]# url=www.baidu.com.cn
[root@localhost scripts]# echo ${url:0:5}
www.b
[root@localhost scripts]# echo ${url:5:5}
aidu.
[root@localhost scripts]# echo ${url:5}
aidu.com.cn

(3)变量内容的替换

[root@localhost scripts]# url=www.baidu.com.cn
[root@localhost scripts]# echo ${url/baidu/sina}
www.sina.com.cn
[root@localhost scripts]# echo ${url//w/N}      贪婪匹配
NNN.baidu.com.cn

(4)变量的替代

${变量名-新的变量值}
变量没有被赋值:会使用"新的变量值"替代
变量有被赋值(包括空值):不会被替代
[root@localhost scripts]# unset var1
[root@localhost scripts]# echo ${var1-aaaa}
aaaa
[root@localhost scripts]# var1=aaaa
[root@localhost scripts]# echo ${var1-bbbb}
aaaa
[root@localhost scripts]# var1=
[root@localhost scripts]# echo ${var1-bbbb}
${变量名-新的变量值}
变量没有被赋值(包括空值):会使用"新的变量值"替代
变量有被赋值:不会被替代
[root@localhost scripts]# var1=
[root@localhost scripts]# echo ${var1:-bbbb}
bbbb
[root@localhost scripts]# var1=aaaa
[root@localhost scripts]# echo ${var1:-bbbb}
aaaa
[root@localhost scripts]# unset var1
[root@localhost scripts]# echo ${var1:-bbbb}
bbbb

转载于:https://www.cnblogs.com/lovelinux199075/p/8876074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值