Starting with bash: -lt and -gt arguments

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

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

Method 3

They are just operators.

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

You can look here for more information on operators:

Method 4

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x