What does the operator `-gt` in shell scripts mean?
$ help test
test: test [expr]
Evaluate conditional expression.
...
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.
-gt
means "greater than". It is used to compare integers for the inequality that is usually written >
in other languages (in some shells, with the test
utility or inside [ ... ]
, >
compares two strings for lexicographical ordering, so it has a very different meaning from -gt
).
-gt
is documented in the manual for test
or [
, or in your shell's manual if these are built-in utilities, as
n1 -gt n2
True if the integer
n1
is algebraically greater than the integern2
; otherwise, false.
(the above is taken from the POSIX standard text about the test
utility)
Fortran also uses this abbreviation in its .GT.
relational operator for numbers.
The other relevant operators for comparing integers in the shell with test
or in [ ... ]
are -ge
("greater-than or equal"), -lt
("less-than"), -le
("less-than or equal"), -eq
("equal") and -ne
("not equal").
Interestingly, all of these are the same in Fortran (.GT.
, .GE.
, .LT.
, .LE.
, .EQ.
and .NE.
).
You can start with help test
, which will display the help of the POSIX subset of the syntax supported by the [[
operator.
A comprehensive documentation is in the CONDITIONAL EXPRESSIONS
section of man bash
.
Specifically:
Other operators:
...
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.