dominique120
dominique120

Reputation: 1302

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

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?

Upvotes: 4

Views: 20972

Answers (2)

devnull
devnull

Reputation: 123608

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

Upvotes: 11

IMSoP
IMSoP

Reputation: 97898

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

Upvotes: 2

Related Questions