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 -eq operator is a binary arithmetic comparison operator in Bash used to evaluate numerical equality between two integer operands. It returns an exit status of 0 (true) if the resolved integer values of both operands are mathematically equal, and 1 (false) otherwise. Syntax The operator requires two operands and must be executed within a test construct, such as the POSIX test command, the standard test brackets [ ], or the extended test brackets [[ ]]:
[ operand1 -eq operand2 ]
[[ operand1 -eq operand2 ]]
test operand1 -eq operand2
Evaluation Contexts The parsing rules, type strictness, and radix resolution of -eq are not inherent to the operator itself; they depend entirely on the enclosing command construct. Standard Test ([ and test)
  • Strict Base-10 Parsing: Operands are evaluated strictly as base-10 integers. Leading zeros are ignored, meaning the literal 010 is parsed as decimal 10, not octal 8.
  • Type Strictness: Passing non-numeric strings or floating-point numbers (e.g., 3.14) triggers an integer expression expected error and immediately returns an exit status of 2.
Extended Test ([[ ]])
  • Arithmetic Evaluation: Operands are evaluated dynamically as arithmetic expressions. Variables do not require the $ prefix for dereferencing.
  • Radix/Base Resolution: Bash integer bases are actively interpreted. A leading 0 denotes an octal value (e.g., 010 evaluates to decimal 8), and a leading 0x denotes hexadecimal.
  • String Handling: Non-numeric strings (e.g., "foo") are treated as variable names. If the referenced variable is unset, it evaluates to 0 without throwing an error. Consequently, [[ "foo" -eq 0 ]] evaluates to true.
  • Floating-Point Error: Because Bash lacks native floating-point support, passing a float (e.g., 3.14) triggers an arithmetic syntax error and returns an exit status of 1.
Exit Status Codes
  • 0: Operands are mathematically equal.
  • 1: Operands are not mathematically equal, or an arithmetic syntax error occurred within [[ ]].
  • 2 (or higher): Evaluation failure within [ or test due to syntax errors, missing operands, or invalid data types.
Contrast with String Equality The -eq operator utilizes a fundamentally different evaluation engine than the = or == operators, which perform lexicographical string comparisons. Because -eq parses operands numerically, it evaluates 05 and 5 as equal. Conversely, the == operator evaluates 05 and 5 as unequal because their literal character sequences differ.
Master Bash with Deep Grasping Methodology!Learn More