Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the gt for here? "if [ $VARIABLE -gt 0 ]; then"

Tags:

linux

bash

shell

What does the -gt mean here:

if [ $CATEGORIZE -gt 0 ]; then

This is part of a bash script I'm working with.

Also, where can I find a list of "flags" that go in there so I can have for reference in the future?

like image 450
dominique120 Avatar asked Oct 16 '25 02:10

dominique120


2 Answers

-gt is an arithmetic test that denotes greater than.

Your condition checks if the variable CATEGORIZE is greater than zero.

Quoting from help test (the [ is a command known as test; help is a shell builtin that provides help on shell builtins):

  arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                 -lt, -le, -gt, or -ge.
  • -eq: Equal
  • -ne: Not equal
  • -lt: Less than
  • -le: Less than or equal to
  • -gt: Greater than
  • -ge: Greater than or equal to

You could also express the condition in an arithmetic context1 by saying:

if ((CATEGORIZE > 0)); then

instead of

if [ $CATEGORIZE -gt 0 ]; then

1 Quoting from help '((':

(( ... )): (( expression ))

Evaluate arithmetic expression.

The EXPRESSION is evaluated according to the rules for arithmetic
evaluation.  Equivalent to "let EXPRESSION".

Exit Status:
Returns 1 if EXPRESSION evaluates to 0; returns 0 otherwise.
like image 146
devnull Avatar answered Oct 17 '25 16:10

devnull


-gt means "greater than", compared arithmetically

[ is (peculiarly) an alias of test (with a mandatory last argument of ], to make it look like a pair of brackets).

bash has its own "builtin" version of [/test, so any bash reference (e.g man bash, info bash, or http://www.gnu.org/software/bash/manual/) will document that, or man [/man test should give you the documentation for the standard standalone version.

Specifically, this page gives an overview of the command, as implemented by bash, and this page lists the available operators.

As well as arithmetic and string tests, you may come across the -e test, for "file exists", as in [ -e /hard/coded/path/$variable_filename ]

bash also includes a slightly extended version, in the form of [[ ... ]].

like image 24
IMSoP Avatar answered Oct 17 '25 16:10

IMSoP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!