linux 中判断变量是否已经赋值

 

001、

[root@PC1 test]# a=10
[root@PC1 test]# echo $a
10
[root@PC1 test]# echo $b

[root@PC1 test]# [ -z $a ]         ## 如果变量没有被赋值,则返回真,否则返回假
[root@PC1 test]# echo $?           ## a赋值为10, 因此返回假
1
[root@PC1 test]# [ -z $b ]   
[root@PC1 test]# echo $?           ## 变量b没有被赋值,返回真
0
[root@PC1 test]# b="dsf"
[root@PC1 test]# echo $b
dsf
[root@PC1 test]# [ -z $b ]         ## 变量b被赋值,返回假
[root@PC1 test]# echo $?
1

 

posted @ 2022-12-08 22:43  小鲨鱼2018  阅读(227)  评论(0编辑  收藏  举报