linux 中[]和[[]]的区别

 

linux 中 []和[[]]都支持逻辑判断,后者比前者功能更强大,比较符合通用的编程规则。

 

001、举一例子

[root@pc1 test1]# a=10
[root@pc1 test1]# b=200
[root@pc1 test1]# if [ $a == 10 ]; then echo "a=10"; fi
a=10
[root@pc1 test1]# if [ $a == 10 && $b == 200 ]; then echo "a=10; b=200"; fi   ## [] 和的逻辑判断 报错
-bash: [: missing `]'
[root@pc1 test1]# if [[ $a == 10 && $b == 200 ]]; then echo "a=10; b=200"; fi   ## [[]]支持一般的编程写法
a=10; b=200
[root@pc1 test1]# if [ $a == 10 ] && [ $b == 200 ]; then echo "a=10; b=200"; fi  ## []需要这样写
a=10; b=200

 

002、例子2

[root@pc1 test1]# a="abc xyz"
[root@pc1 test1]# echo $a
abc xyz
[root@pc1 test1]# if [ $a == "abc xyz" ]; then echo "a=abc xyz"; fi      ## []报错
-bash: [: too many arguments
[root@pc1 test1]# if [[ $a == "abc xyz" ]]; then echo "a=abc xyz"; fi    ## [[]]可以正常比较
a=abc xyz

 

参考:

01、https://blog.csdn.net/transformer_WSZ/article/details/126531769

 

posted @ 2024-01-01 10:27  小鲨鱼2018  阅读(80)  评论(0编辑  收藏  举报