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.
-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 thetest command, the POSIX-compliant single bracket [ ], or the Bash-specific extended test keyword [[ ]].
Technical Mechanics
- Operand Constraints: The
-leoperator strictly requires integer evaluation; Bash does not natively support floating-point arithmetic. The behavior upon encountering invalid types depends on the context:- In
[ ]andtest: 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 of2. - 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 to0. A syntax error is only triggered if the string contains fundamentally invalid arithmetic syntax (e.g.,"a b"or2.5), yielding an exit status of1.
- In
- 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 aninteger expression expectederror. - In the extended
[[ ]]context, an unset variable referenced strictly by name (e.g.,[[ var -le 2 ]]) implicitly evaluates to the integer0. 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).
- In the POSIX
- Base Representation:
- In
[ ]andtest: Operands are strictly parsed as base 10. A leading0does not trigger octal interpretation (e.g.,010is evaluated as10), and hexadecimal prefixes (e.g.,0x10) result in aninteger expression expectederror. - In
[[ ]]: Because operands are subjected to arithmetic evaluation, standard Bash integer bases apply. A leading0is interpreted as octal, and a leading0xis interpreted as hexadecimal.
- In
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.
[ ] 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





