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

 

001、

root@PC1:/home/test2# ls
root@PC1:/home/test2# a=abd
root@PC1:/home/test2# echo $a
abd
root@PC1:/home/test2# [ $a =~ ^a ] && echo yes      ## 单个[]不支持匹配
-bash: [: =~: binary operator expected
root@PC1:/home/test2# [[ $a =~ ^a ]] && echo yes    ## 双个[[]]支持匹配
yes

 

002、

root@PC1:/home/test2# a=100
root@PC1:/home/test2# b=500
root@PC1:/home/test2# echo $a $b
100 500
root@PC1:/home/test2# [ $a -gt 0 && $b -gt 0 ] && echo positive || echo negative
-bash: [: missing `]'
negative
root@PC1:/home/test2# [[ $a -gt 0 && $b -gt 0 ]] && echo positive || echo negative  ## 双[[]]通用性好
positive

[[]]的通用性更好

posted @ 2022-05-23 21:07  小鲨鱼2018  阅读(858)  评论(0编辑  收藏  举报