24

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!

  • 9
    Sorry, but I feel worth pointing out that the echo statements in the code answer your question. – devnull Mar 13 '14 at 16:13
  • There are missing quotes around the variables – Stéphane Chazelas Mar 13 '14 at 16:15
29

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

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.

4

They are just operators.

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

You can look here for more information on operators:

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.