TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-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.Technical Characteristics
- Strictly Integer-Based: Bash does not natively support floating-point arithmetic. The
-gtoperator 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
[andtest, it yields aninteger expression expectederror and an exit status of2. - In the extended
[[ ]]construct, operands undergo arithmetic evaluation. A float produces asyntax error: invalid arithmetic operatorand yields an exit status of1.
- In
- Evaluation in
[ ]vs[[ ]]:- Standard
[ ]andtest: 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 aninteger expression expectederror. - Extended
[[ ]]: Because operands of-gtundergo 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 to0instead of throwing a syntax error.
- Standard
- Lexicographical vs. Arithmetic: The
-gtoperator 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
-gtoperator 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[ortestconstruct.
Master Bash with Deep Grasping Methodology!Learn More





