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.
-ge operator is a binary integer comparison operator in Bash that evaluates whether the left operand is mathematically greater than or equal to 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 thetest builtin, the POSIX test command ([), or the Bash extended test keyword ([[).
Technical Characteristics
The behavior of the-ge operator diverges significantly depending on the enclosing test construct:
- Arithmetic Evaluation:
- Within the POSIX
[construct, operands are strictly parsed as literal integers. - Within the extended
[[construct, operands to-geundergo arithmetic evaluation before comparison. This means mathematical expressions are dynamically resolved, making syntax like[[ "1+2" -ge 3 ]]perfectly valid.
- Within the POSIX
- Type Constraints and Error Handling: Bash does not natively support floating-point arithmetic, but the resulting errors depend on the construct used.
- In
[: Passing a non-integer string or a float to-gecauses Bash to throw aninteger expression expectederror. - In
[[: Passing a float (e.g.,"1.5") throws asyntax error: invalid arithmetic operator. However, a non-integer string (e.g.,"foo") is treated as a variable name due to the arithmetic evaluation context.
- In
- Variable Expansion and Empty Strings:
- In
[: Operands should be quoted (e.g.,[ "$a" -ge "$b" ]). If an unquoted variable is empty or unset, the shell expands it to nothing. This leaves the command missing its left operand (e.g.,[ -ge 5 ]), causing[to parse-geincorrectly and throw a specific command error:bash: [: -ge: unary operator expected. If quoted and empty ([ "" -ge 5 ]), it throws aninteger expression expectederror. - In
[[with$var: If an unset or empty variable is referenced with a$(e.g.,[[ $a -ge 0 ]]), it expands to an empty string"". The arithmetic evaluator cannot process an empty string and throws a syntax error:bash: [[: : syntax error: operand expected (error token is ""). - In
[[with unquoted names: If a variable is referenced by name without the$(e.g.,[[ a -ge 0 ]]), its behavior depends strictly on its state. If the variable is explicitly set to an empty string (a=""), Bash throws the exact samesyntax error: operand expected. Only an unset variable referenced by name (or a variable containing a string that matches an unset variable’s name) safely evaluates to0without throwing an error.
- In
- Contextual Limitation:
-geis specifically designed for thetest([) and extended test ([[) constructs. It is not used within arithmetic evaluation contexts ((( ))); the arithmetic equivalent is the>=operator.
Evaluation Matrix
The following table demonstrates the evaluation of various expressions using the-ge operator:
| Expression | Exit Status | Boolean Result | Notes |
|---|---|---|---|
[[ 10 -ge 5 ]] | 0 | True | Standard integer comparison. |
[[ 5 -ge 5 ]] | 0 | True | Equality satisfies the condition. |
[[ 5 -ge 10 ]] | 1 | False | Left operand is lesser. |
[[ -2 -ge -5 ]] | 0 | True | Handles negative integers. |
[[ "2+3" -ge 5 ]] | 0 | True | Arithmetic evaluation resolves "2+3" to 5. |
[[ unset_var -ge 0 ]] | 0 | True | Unset variable referenced by name evaluates to 0. |
Operator Equivalency
To understand-ge within the broader Bash operator ecosystem, it is the arithmetic counterpart to string comparison operators, but strictly for numeric evaluation:
Master Bash with Deep Grasping Methodology!Learn More





