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 -ne (not equal) operator is a binary arithmetic comparison operator in Bash used to evaluate whether two integer operands have different numeric values. It returns an exit status of 0 (true) if the integers are strictly not equal, and 1 (false) if they are mathematically equal.

Syntax

The operator requires whitespace on both sides and must be executed within a test construct or the test builtin:
[ INT1 -ne INT2 ]
[[ INT1 -ne INT2 ]]
test INT1 -ne INT2

Technical Characteristics

  • Evaluation Context and Type Constraints: The -ne operator forces an arithmetic evaluation context, but its handling of non-numeric strings depends entirely on the enclosing construct:
    • Inside [ ] and test: Both operands must strictly resolve to integers. If an operand is a non-numeric string or empty, Bash throws an integer expression expected runtime evaluation error. The command outputs the error message to standard error and returns an exit status of 2. If the errexit option (set -e) is enabled, this non-zero exit status will immediately terminate the script unless the test is evaluated as part of a control structure.
    • Inside [[ ]]: The operator evaluates its operands as arithmetic expressions (equivalent to the evaluation performed inside $(( ))). Strings containing mathematical operations (e.g., "1 + 1") are computed prior to comparison. If an operand is an unexpanded string that forms a valid identifier (e.g., [[ var -ne 0 ]]), Bash evaluates the variable’s contents. If the variable is unset or explicitly initialized with an empty string (var=""), it evaluates to 0 in this arithmetic context (though an unset variable will trigger an unbound variable error if set -u is enabled). Conversely, if the variable is explicitly expanded and results in an empty string (e.g., [[ "$var" -ne 0 ]]), Bash encounters a missing operand and throws an arithmetic syntax error (operand expected).
  • Base Handling: When used inside the extended test construct [[ ]], operands with leading zeros may be evaluated as octal, and arithmetic expressions are evaluated directly. Inside POSIX single brackets [ ], operands are treated strictly as base-10 integers.
  • Exit Status Mapping:
    • INT1 != INT2 → Exit code 0 (True)
    • INT1 == INT2 → Exit code 1 (False)

-ne vs. !=

In Bash, the operator itself defines the comparison type. It is critical to distinguish -ne from !=:
  • -ne performs arithmetic comparison. It evaluates the mathematical value (e.g., 05 and 5 are equal, so -ne returns 1).
  • != performs lexicographical (string) comparison. It evaluates the exact character sequence (e.g., "05" and "5" are different strings, so != returns 0).

# Arithmetic evaluation: 05 and 5 are mathematically equal.

# Exit status: 1 (False)
[ 05 -ne 5 ]


# Lexicographical evaluation: "05" and "5" are different strings.

# Exit status: 0 (True)
[ "05" != "5" ]
Master Bash with Deep Grasping Methodology!Learn More