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 -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 the test builtin, the POSIX test command ([), or the Bash extended test keyword ([[).

# POSIX standard test syntax
[ INT1 -ge INT2 ]
test INT1 -ge INT2


# Bash extended test syntax
[[ INT1 -ge INT2 ]]

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 -ge undergo arithmetic evaluation before comparison. This means mathematical expressions are dynamically resolved, making syntax like [[ "1+2" -ge 3 ]] perfectly valid.
  • 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 -ge causes Bash to throw an integer expression expected error.
    • In [[: Passing a float (e.g., "1.5") throws a syntax error: invalid arithmetic operator. However, a non-integer string (e.g., "foo") is treated as a variable name due to the arithmetic evaluation context.
  • 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 -ge incorrectly and throw a specific command error: bash: [: -ge: unary operator expected. If quoted and empty ([ "" -ge 5 ]), it throws an integer expression expected error.
    • 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 same syntax 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 to 0 without throwing an error.
  • Contextual Limitation: -ge is specifically designed for the test ([) 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:
ExpressionExit StatusBoolean ResultNotes
[[ 10 -ge 5 ]]0TrueStandard integer comparison.
[[ 5 -ge 5 ]]0TrueEquality satisfies the condition.
[[ 5 -ge 10 ]]1FalseLeft operand is lesser.
[[ -2 -ge -5 ]]0TrueHandles negative integers.
[[ "2+3" -ge 5 ]]0TrueArithmetic evaluation resolves "2+3" to 5.
[[ unset_var -ge 0 ]]0TrueUnset 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:

# Integer comparison (Greater than or equal to)
[[ "$int1" -ge "$int2" ]]


# String comparison (Lexicographical greater than or equal to)

# Note: Requires escaping in [ ] but not in [[ ]]
[[ "$str1" > "$str2" || "$str1" == "$str2" ]]


# Arithmetic context equivalent
(( int1 >= int2 ))
Master Bash with Deep Grasping Methodology!Learn More