What does the operator `-gt` in shell scripts mean?

26,814

Solution 1

$ 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.

Solution 2

-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 integer n2; 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.).

Solution 3

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.
Share:
26,814

Related videos on Youtube

Sergio
Author by

Sergio

Updated on September 18, 2022

Comments

  • Sergio
    Sergio almost 2 years

    Hi I have this sentence and I want to know what does it means please.

    if [[ -z "$1" ]]; then   #  --> this is if the value of the parameter $1 is zero
        PASO=1
    elif [[ "$1" -gt 1 ]] ; then  # but i don't know what this flags mean? .."-gt"
        LOG "[$(date +%T)] Parametros incorrectos"
        exit 255
    else
        PASO=$1
    fi
    

    What does -gt mean?

    • ron
      ron about 6 years
    • dave_thompson_085
      dave_thompson_085 about 6 years
      -z tests for string length zero, also called 'empty' or 'null', but not the string consisting of one character 0 used to represent the integer zero.
  • Sergio
    Sergio about 6 years
    Yes I know that... what i need to know is what does it means " -gt " :-D
  • Angel Todorov
    Angel Todorov about 6 years
    Read that section of the man page, it's documented right in there.
  • schily
    schily about 6 years
    On a UNIX system help testresults in the message: ERROR: Key 'test' not found (he1) ;-)
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 6 years
    @schily There are so many UNIX systems, and then so many more non-UNIX systems that most "UNIX" systems probably aren't. help is a bash built-in command and as such has not much to do with UNIX (as opposed to man which is usually an executable and was probably about the third command conceived during the development of UNIX, after sh and cc). If you try man test you'll get a description of a command which you probably never execute but which is helpful anyway because the various shell built-ins faithfully emulate its original behavior.
  • schily
    schily about 6 years
    help is a command that is part of SCCS. Shells that introduced a builtin named help with different behavior show that their author did not try to inform himself about existing practice before creating that builtin.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 6 years
    I'd say "display the help of the POSIX subset of the syntax supported by the [ operator" because [[...]] is actually part of the bash grammar, while /bin/[ is a POSIX command.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 6 years
    @schily On the contrary: Any proprietary software offering executables named help (or test, or exit, or any other of a number of common English words) is immediately self-disqualified because they likely didn't think much about a number of other critical decisions either.
  • schily
    schily about 6 years
    Well, the help builtin in bash is proprietary, the external helpcommand is UNIX standard software since 1977. So bash disqualifies itself.
  • Angel Todorov
    Angel Todorov about 6 years
    OK, help is problematic. man test and/or gnu.org/software/bash/manual/…
  • bobah
    bobah about 6 years
    @PeterA.Schneider - /bin/[ is a command line tool, [ is a Bash built-in, and [[ is a Bash operator. With my statement I wanted to emphasize that the [[ is a Bash extension of the POSIX [.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica about 6 years
    @schily I admit I was cocky. For example I didn't know that sccs and Unix were so close. I wrote a question regarding sccs help.