Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The -gt (greater than) operator is a binary arithmetic comparison operator in Bash used to evaluate whether the integer value of the left operand is strictly greater than the integer value of the right operand. It yields an exit status of 0 (true) if the condition is satisfied, and 1 (false) otherwise.

Syntax

The operator is utilized within test constructs. It requires spaces around the operator and the operands.

# POSIX test command
test INT1 -gt INT2


# POSIX test bracket syntax
[ INT1 -gt INT2 ]


# Bash extended test keyword
[[ INT1 -gt INT2 ]]

Technical Characteristics

  • Strictly Integer-Based: Bash does not natively support floating-point arithmetic. The -gt operator evaluates integers only. Passing a floating-point number (e.g., 3.14) results in an error, but the specific error and exit status depend on the test construct used:
    • In [ and test, it yields an integer expression expected error and an exit status of 2.
    • In the extended [[ ]] construct, operands undergo arithmetic evaluation. A float produces a syntax error: invalid arithmetic operator and yields an exit status of 1.
  • Evaluation in [ ] vs [[ ]]:
    • Standard [ ] and test: Variables must be explicitly dereferenced ($VAR) and should be double-quoted ("$VAR") to prevent syntax errors if the variable is null or unset. Passing a non-numeric string results in an integer expression expected error.
    • Extended [[ ]]: Because operands of -gt undergo arithmetic evaluation inside [[ ]], variables can be referenced by name without the $ prefix (e.g., [[ var -gt 5 ]]). Consequently, if a non-numeric string is passed, it is treated as a variable name. If that variable is unset, it silently evaluates to 0 instead of throwing a syntax error.
  • Lexicographical vs. Arithmetic: The -gt operator is exclusively for arithmetic comparison. It must not be confused with the > operator, which performs lexicographical (string) comparison when used inside [[ ]] or [ ] (where it must be escaped as \>).
  • Exclusion from Arithmetic Contexts: The -gt operator is not valid inside Bash arithmetic evaluation constructs (( )) or $(( )). In those specific contexts, the standard C-style > operator is used for integer comparison.

Exit Status Codes

The operator relies on standard POSIX exit codes to communicate the result of the evaluation to the shell:
  • 0 (Success/True): The left integer is strictly greater than the right integer.
  • 1 (Failure/False/Arithmetic Error): The left integer is less than or equal to the right integer. This exit status is also returned when an arithmetic evaluation error occurs inside a [[ ]] construct (e.g., attempting to evaluate a floating-point number).
  • 2 (Syntax/Operand Error): An invalid operand was provided (e.g., a missing operand, a float, or a non-numeric string) specifically within a standard [ or test construct.
Master Bash with Deep Grasping Methodology!Learn More