String Comparison
[[ str1 == pattern ]] str1 = pattern,
[[ str1 = pattern ]] Same as == above, but is obsolete
[[ str1 != pattern ]] str1 != pattern
[[ str1 < str2 ]] str1 precedes str2
[[ str1 > str2 ]] str1 follow str2
[[ -z str ]] str is null, it includes following 3 format:
str=
str=""
unset str
[[ -n str ]] str is not null
Sample 1
[[ ${str} == ABC* ]]; # str start with ABC
[[ ${str} = *DEF ]]; # str end with DEF
[[ ${str} != *DEF ]]; # str NOT end with DEF
[[ ${str} = @(ABC*|*DEF) ]]; # str start with ABC or end with DEF
[[ ${str} == ABC* || ${str} == *DEF) ]]; # str start with ABC or end with DEF
Sample 2
VAR1="12"
VAR2="34"
VAR3="12_34"
VARID="3"
if [[ ${VAR1}_${VAR2} = @(${VAR3}) ]]; then
if eval [[ ${VAR1}_${VAR2} = @(\$\{VAR${VARID}\}) ]]; then
Number Comparison
(( num1 == num2 )) numbers equal
(( num1 != num2 )) numbers not equal
(( num1 < num2 )) num1 < num2
(( num1 > num2 )) num1 > num2
(( num1 <= num2 )) num1 <= num2
(( num1 >= num2 )) num1 >= num2
The following obsolete arithmetic comparisons are also permitted:
exp1 -eq exp2 exp1 is equal to exp2.
exp1 -ne exp2 exp1 is not equal to exp2.
exp1 -lt exp2 exp1 is less than exp2.
exp1 -gt exp2 exp1 is greater than exp2.
exp1 -le exp2 exp1 is less than or equal to exp2.
exp1 -ge exp2 exp1 is greater than or equal to exp2.
File Comparison
-c file True, if file exists and is a character special file.
-d file True, if file exists and is a directory.
-e file True, if file exists.
-f file True, if file exists and is an ordinary file.
-r file True, if file exists and is readable by current process.
-s file True, if file exists and has size greater than zero.
-w file True, if file exists and is writable by current process.
-x file True, if file exists and is executable by current process.
-L file True, if file exists and is a symbolic link.
-h file True, if file exists and is a symbolic link.
Logical Operator
[[ expr1 && expr2 ]]
[[ expr1 || expr2 ]]
[[ ! expr1 ]]