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 -le operator is a binary integer comparison operator in Bash that evaluates whether the left operand is mathematically less than or equal to the right operand. It returns an exit status of 0 (true) if the condition is met, and 1 (false) if the left operand is strictly greater than the right operand.

Syntax

The operator is utilized within the test command, the POSIX-compliant single bracket [ ], or the Bash-specific extended test keyword [[ ]].

# POSIX-compliant test command
test INT1 -le INT2


# POSIX-compliant single bracket
[ INT1 -le INT2 ]


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

Technical Mechanics

  • Operand Constraints: The -le operator strictly requires integer evaluation; Bash does not natively support floating-point arithmetic. The behavior upon encountering invalid types depends on the context:
    • In [ ] and test: Operands must be strictly integer literals. Passing a floating-point number (e.g., 2.5) or a non-numeric string results in a runtime error (bash: [: <value>: integer expression expected) and yields an exit status of 2.
    • In [[ ]]: Operands undergo arithmetic evaluation. Passing a standard non-numeric string (e.g., "abc") does not inherently throw an error; instead, Bash treats the string as a variable name within an arithmetic context. If the variable is unset, it silently evaluates to 0. A syntax error is only triggered if the string contains fundamentally invalid arithmetic syntax (e.g., "a b" or 2.5), yielding an exit status of 1.
  • Empty Variable Handling:
    • In the POSIX [ ] context, an unquoted empty variable results in a syntax error due to a missing expected argument. A quoted empty string ("") yields an integer expression expected error.
    • In the extended [[ ]] context, an unset variable referenced strictly by name (e.g., [[ var -le 2 ]]) implicitly evaluates to the integer 0. Conversely, an empty string literal or an expanded empty variable (e.g., [[ "" -le 2 ]] or [[ $var -le 2 ]]) causes an arithmetic syntax error (bash: [[: : syntax error: operand expected).
  • Base Representation:
    • In [ ] and test: Operands are strictly parsed as base 10. A leading 0 does not trigger octal interpretation (e.g., 010 is evaluated as 10), and hexadecimal prefixes (e.g., 0x10) result in an integer expression expected error.
    • In [[ ]]: Because operands are subjected to arithmetic evaluation, standard Bash integer bases apply. A leading 0 is interpreted as octal, and a leading 0x is interpreted as hexadecimal.

Evaluation Differences vs. Arithmetic Expansion

While -le serves the same logical purpose as the <= operator used within Bash’s arithmetic expansion context (( )), their evaluation mechanics differ significantly.
[ "$a" -le "$b" ]
(( a <= b ))
In the [ ] construct, -le strictly requires integer literals. If a variable contains an arithmetic expression (e.g., a="1+1"), [ ] will throw an error. In contrast, (( )) performs full arithmetic evaluation, successfully resolving 1+1 to 2 before the comparison. Furthermore, (( )) treats non-numeric strings as variable names or 0, whereas [ ] throws an error. Note: Do not confuse -le with the < or > operators inside [[ ]]. The -le operator performs mathematical integer comparisons, whereas < and > perform lexicographical (string) comparisons based on the current locale’s collating sequence. Bash does not support a <= operator inside [[ ]].
Master Bash with Deep Grasping Methodology!Learn More