linux 中根据变量是否为空为变量赋值

发布时间 2023-06-15 00:30:21作者: 小鲨鱼2018

 

001、当变量为空时,为变量赋值

[root@PC1 test2]# var
bash: var: command not found...
[root@PC1 test2]# echo $var

[root@PC1 test2]# var=${var:-123456}     ## 变量为空时,将变量的值赋值为123456
[root@PC1 test2]# echo $var
123456

 

002、当变量有值时

[root@PC1 test2]# var=5000
[root@PC1 test2]# echo $var                        ## 变量有已知值
5000
[root@PC1 test2]# var=${var:-123456}               ## 此时变量不能被赋值
[root@PC1 test2]# echo $var
5000