0
点赞
收藏
分享

微信扫一扫

变量的替代

转角一扇门 2022-03-21 阅读 33

变量替代

1.${变量名-新的变量值}

2.${变量名:-新的变量值}


${变量名-新的变量值}

1.变量没有被赋值的情况下

[root@xiudaochengxian test]# unset url
[root@xiudaochengxian test]# echo $url
[root@xiudaochengxian test]# url=www.xiuxian.com
[root@xiudaochengxian test]# echo $url
www.xiuxian.com
[root@xiudaochengxian test]# echo ${url-www.baidu.com}
www.xiuxian.com
[root@xiudaochengxian test]# unset url
[root@xiudaochengxian test]# echo ${url-www.baidu.com}
www.baidu.com

2.变量被赋值(包括空值)的情况下

[root@xiudaochengxian test]# url2=
[root@xiudaochengxian test]# echo ${url2-www.baidu.com}

.${变量名:-新的变量值}

1.变量没有被赋值(包括空值)的情况下

[root@xiudaochengxian test]# unset url
[root@xiudaochengxian test]# echo $url
[root@xiudaochengxian test]# url=www.xiuxian.com
[root@xiudaochengxian test]# echo $url
www.xiuxian.com
[root@xiudaochengxian test]# echo ${url:-www.baidu.com}
www.xiuxian.com
[root@xiudaochengxian test]# unset url
[root@xiudaochengxian test]# echo ${url:-www.baidu.com}
www.baidu.com

2.变量被赋值的情况下:不会被替代

[root@xiudaochengxian test]# url2=
[root@xiudaochengxian test]# echo $url2
[root@xiudaochengxian test]# echo ${url2:-www.baidu.com}
www.baidu.com
举报

相关推荐

0 条评论