普通的test可以用[]来代替,
compound test可以用[[]]代替。
如果是compound test就可以理解为metacharecter matching的支持,此时如果要精确的比较,就要加上双引号。
很奇怪的是,在fedora12上,
$name = zhangqing
$[ $name = [zZ]hangqing ]
$echo $? 0
$[ $name=[zZ]hangqing ]
$echo $? 1
实验是证明一定要用空格隔开
经过确认:
Test Operator | Tests True If |
---|---|
String Test | |
[ string1 = string2 ] [ string1==string2 ] | String1 is equal to String2 (space surrounding = required). (Can be used instead of the single = sign on bash versions 2.x.) |
[ string1 != string2 ] | String1 is not equal to String2 (space surrounding != required). |
[ string ] | String is not null. |
[ –z string ] | Length of string is zero. |
[ –n string ] | Length of string is nonzero. |
[ –l string ] | Length of string (number of characters). |
EXAMPLE test –n $word or [ –n $word ] | |
Logical Test | |
[ string1 –a string1 ] | Both string1 and string2 are true. |
[ string1 –o string2 ] | Either string1 or string2 is true. |
[ ! string1 ] | Not a string1 match. |
Logical Test (Compound Test)[a] | |
[[ pattern1 && pattern2 ]] | Both pattern1 and pattern2 are true. |
[[ pattern1 || pattern2 ]] | Either pattern1 or pattern2 is true. |
[[ ! pattern ]] | Not a pattern match. |
Integer Test | |
[ int1 –eq int2 ] | Int1 is equal to int2. |
[ int1 –ne int2 ] | Int1 is not equal to int2. |
[ int1 –gt int2 ] | Int1 is greater than int2. |
[ int1 –ge int2 ] | Int1 is greater than or equal to int2. |
[ int1 –lt int2 ] | Int1 is less than int2. |
[ int1 –le int2 ] | Int1 is less than or equal to int2. |
Binary Operators for File Testing | |
[ file1 –nt file2 ] | True if file1 is newer than file2 (according to modification date). |
[ file1 –ot file2 ] | True if file1 is older than file2. |
[ file1 –ef file2 ] | True if file1and file2 have the same device or inode numbers. |
shopt 用来设置一些shell的属性
[[ $name == [Tt]o+(m)y ]]
start with T/t, followed 1 or n m, end with y.
test -n -----not-null