linux中测试变量是不是已经设置



linux中测试变量是不是已经设置
 
1.${test:-newvalue}
${test:-newvalue}主要就是测试test这个变量是不是已经定义了,如果已经定义了,那么还是
打印这个定义了的值;但是如果没有的话,那么将打印这个newvalue的值,但是不会将这个
值赋值给test变量。
  www.2cto.com  
例子:
[root@redhat ~]# test="blue"
[root@redhat ~]# echo "the sky is ${test:-grey} today";
the sky is blue today
[root@redhat ~]# unset test
[root@redhat ~]# echo "the sky is ${test:-grey} today";
the sky is grey today
  www.2cto.com  
[root@redhat ~]# unset test
[root@redhat ~]# echo "the sky is ${test:-grey} today";
the sky is grey today
[root@redhat ~]# echo ${test}   #这里是没有设置test变量的
  www.2cto.com  
2.${test:=newvalue}
下面来看看${test:=newvalue}
[root@redhat ~]# echo "the sky is ${test:=grey} today";
the sky is grey today
[root@redhat ~]# echo ${test}
grey  
#很显然的,${test:=newvalue}不光会在没有设置test变量的时候打印出newvalue的值,
而且还会将newvalue的值赋值给test变量。
 

相关内容

    暂无相关文章