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
[[]]的通用性更好。

浙公网安备 33010602011771号