Starting with bash: -lt and -gt arguments

272,767

Solution 1

It's short for less than and greater than. It's used for integer comparison in bash. You can read more by typing man test:

   ....
   INTEGER1 -gt INTEGER2
          INTEGER1 is greater than INTEGER2
   ....
   INTEGER1 -lt INTEGER2
          INTEGER1 is less than INTEGER2
   ....

Solution 2

You can find the definition of -lt and -gt in the documentation of the test command (man test), or in the documentation of bash since test is a built-in command in bash (like in most other shells).

-lt and -gt are numeric comparisons (less-than [and not equal], greater-than [and not equal]). There are also less/greater-or-equal operators -le and -ge, and equal and not-equal operators -eq and -ne. These are numeric operators, so there will be an error if either side isn't a number, and 9 is considered less than 10.

The reason names like -lt are used rather than the usual < is that the character < would be interpreted as a redirection. The operators = and != also exist, but they perform a string comparison: test 00 -eq 0 is true whereas test 00 = 0 is false.

Some shells, including bash, also have operators < and > which perform a string lexicographic comparison, so test 9 \< 10 is false because 9 is sorted before 1 (the backslash prevents the character < from being interpreted as a redirection operator). These shells also offer the double-bracket syntax for tests, e.g. [[ 9 < 10 ]] (as opposed to [ 9 \< 10 ]), which can't have redirections inside so the < doesn't need to be quoted.

Solution 3

These are comparison operators

-lt = less than

-gt = greater than

You can check this page for further details:

http://tldp.org/LDP/abs/html/comparison-ops.html

Solution 4

They are just operators.

Simply: gt and lt mean > (greater than) and < (less than).

You can look here for more information on operators:

Share:
272,767

Related videos on Youtube

user47579
Author by

user47579

Updated on September 18, 2022

Comments

  • user47579
    user47579 almost 2 years

    I'm starting with bash and I found the following:

    if test $first -lt $second
    then
      echo $first is lower than $second
    else
      if test $first -gt $second
      then
        echo $first is higher than $second
      else
        echo $first and $second are equals
      fi
    fi
    

    For reading the script and executing it, I know what it does, but not what -lt and -gt are for.

    Can somebody tell me what is the name of that kind of 'tool' and what they(-lt and -gt) do? Thanks!