A binary comparison operator compares two variables or quantities.
Note that integer and string comparison
Use a different set of operators.
1. Integer comparison
-EQIs equal
If ["$ A"-EQ "$ B"]
-NeIs not equal
If ["$ A"-ne "$ B"]
-GTIs greater
If ["$ A"-GT "$ B"]
-GeIs greater than or equal
If ["$ A"-ge "$ B"]
-LtIs less
If ["$ A"-lt "$ B"]
-LeIs less than or equal
If ["$ A"-Le "$ B"]
<Is less than (within double parentheses)
("$ A" <"$ B "))
<=Is less than or equal to (within double parentheses)
("$ A" <= "$ B "))
>Is greater than (within double parentheses)
("$ A"> "$ B "))
> =Is greater than or equal to (within double parentheses)
("$ A"> = "$ B "))
2. String comparison
=Is equal
If ["$ A" = "$ B"]
Note the whitespace framing the =.
If ["$ A" = "$ B"] is not equivalent to the above.
=Is equal
If ["$ A" = "$ B"]
This is a synonym for =.
The = comparison operator behaves differently within a double-brackets test
Within single brackets.
[[$ A = z *]
# True if $ A starts with an "Z" (pattern matching ).
[[$ A = "z *"] # True if $ A is equal to z * (literal matching ).
[$ A = z *]
# File globbing and word splitting take place.
["$ A" = "z *"] # True if $ A is equal to z * (literal matching ).
! =Is not equal
If ["$ "! = "$ B"]
This operator uses pattern matching within a [[...] construct.
<Is less than, in ASCII alphabetical order
If [["$ A" <"$ B"]
If ["$ A" \ <"$ B"]
Note that the "<" needs to be escaped within a [] construct.
>Is greater than, in ASCII alphabetical order
If [["$ A"> "$ B"]
If ["$ A" \> "$ B"]
Note that the ">" needs to be escaped within a [] construct.
-ZString is null, that is, has zero length
String =''
# Zero-length ("null") string variable.
If [-z "$ string"]
Then
Echo "\ $ string is null ."
Else
Echo "\ $ string is not null ."
Fi
# $ String is null.
-NString is not null.
The-n test requires that the string be quoted within the test brackets. Using
Unquoted string! -Z, or even just the unquoted string alone within test brackets
Normally works, however, this is an unsafe practice. always quote
Tested string. [37]