- 对于大多数的shell,[与test同义。[是一个程序,在/usr/bin/[
[[是一个keyword,只在ksh和bash支持。 - [[与[的一些区别:
Feature
new test [[
old test [
Example
string comparison
>
>
-
<
<
-
= (or ==)
=
-
!=
!=
-
expression grouping
&&
-a
[[ -n $var && -f $var ]] && echo "$var is a file"
||
-o
-
Pattern matching
= (or ==)
(not available)
[[ $name = a* ]] || echo "name does not start with an 'a': $name"
RegularExpression matching
=~
(not available)
[[ $(date) =~ ^Fri ... 13 ]] && echo "It's Friday the 13th!"
- [[没有glob展开,所以条件测试时变量不必加引号
file="file1 file2" [ -e "$file" ] #必须加双引号 [[ -e $file ]]
- [[中的括号不必转义了
[[ -f $file1 && ( -d $dir1 || -d $dir2) ]] [ -f "$file1" -a ( -d "$dir1" -o -d "$dir2" ) ]
参考资料: