shell中[ ] 和[[ ]]的区别

1. 空字符串判断

[root@localhost ~]# a=
[root@localhost ~]# echo $a

[root@localhost ~]# [ $a = hello ];echo $?
-bash: [: =: unary operator expected
2
[root@localhost ~]# [ "$a" = "hello" ];echo $?
1
[root@localhost ~]# [[ $a = hello ]];echo $?
1
[root@localhost ~]# 

[ ]在判断空字符串的时候需要加" "
[[ ]]在判断空字符串的时候不加" "也可以

2. 逻辑运算符

[root@localhost ~]# [ -e test -a -L test ];echo $?
1
[root@localhost ~]# [ -e test && -L test ];echo $?
-bash: [: missing `]'
2
[root@localhost ~]# [[ -e test && -L test ]];echo $?
1
[root@localhost ~]# 

用逻辑运算符&&或者|| 连接两个表达式的时候,一个[ ]不可以,但是[[ ]]就可以

发布了168 篇原创文章 · 获赞 1 · 访问量 3002

猜你喜欢

转载自blog.csdn.net/yrx420909/article/details/104343709