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.
-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 [[ ]]:
-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
010is parsed as decimal10, not octal8. - Type Strictness: Passing non-numeric strings or floating-point numbers (e.g.,
3.14) triggers aninteger expression expectederror and immediately returns an exit status of2.
[[ ]])
- 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
0denotes an octal value (e.g.,010evaluates to decimal8), and a leading0xdenotes hexadecimal. - String Handling: Non-numeric strings (e.g.,
"foo") are treated as variable names. If the referenced variable is unset, it evaluates to0without 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 of1.
0: Operands are mathematically equal.1: Operands are not mathematically equal, or an arithmetic syntax error occurred within[[ ]].2(or higher): Evaluation failure within[ortestdue to syntax errors, missing operands, or invalid data types.
-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





