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 == operator in Bash is a binary comparison operator whose behavior—evaluating literal string equality, pattern matching, or numeric equivalence—is strictly dictated by the evaluation context ([[ ]], [ ], or (( ))) enclosing it.

Execution Contexts

1. Extended Test Keyword ([[ ]])

Within the Bash-specific [[ ]] construct, the == operator performs pattern matching (globbing) and string equality. The exact behavior depends on the quoting of the right-hand operand and shell options.

# Syntax: Pattern Matching / Partial Quoting
[[ "$left_operand" == "$prefix"* ]]


# Syntax: Literal String Equality
[[ "$left_operand" == "$exact_match" ]]
  • Quoting and Partial Quoting: The right-hand operand is evaluated dynamically based on quotes. Characters enclosed in single (') or double (") quotes are forced to match as literal strings. Unquoted characters are evaluated as glob patterns (*, ?, [...]). This allows for partial quoting, where variables or literal strings can be safely concatenated with unquoted wildcards.
  • Case Sensitivity (nocasematch): By default, the == operator performs case-sensitive matching. Enabling the nocasematch shell option (shopt -s nocasematch) alters the operator’s behavior within [[ ]] to evaluate both literal strings and patterns case-insensitively.

2. Arithmetic Evaluation ((( )), $(( )), let)

Within arithmetic contexts, the == operator performs numeric equivalence. It evaluates the mathematical value of the operands rather than their character sequence.

# Syntax: Numeric Equivalence
(( left_operand == right_operand ))
  • Numeric Parsing: Because it evaluates mathematical integers, differing string representations of the same number (e.g., leading zeros, evaluated variables, or different bases) evaluate as equal. For example, (( "01" == "1" )) evaluates to true, whereas the same operands in a string context ([[ "01" == "1" ]]) evaluate to false.

3. POSIX Test Command ([ ] or test)

Within the standard [ ] construct, the == operator strictly performs literal string equality. Pattern matching is not supported in this context.

# Syntax: Literal String Equality
[ "$left_operand" == "$right_operand" ]
Note: While Bash parses == in [ ] as a valid synonym for string equality, the single = operator is the strict POSIX standard for the test command. The == operator is a Bash extension.

Evaluation Mechanics

  • Whitespace Sensitivity: In test constructs ([ ] and [[ ]]), operands must be separated from the == operator by whitespace. Omitting whitespace (e.g., [[ "$a"=="$b" ]]) causes Bash to parse the entire expression as a single, non-empty string. Because Bash evaluates any non-empty string as true, this results in a silent false-positive test.
  • Exit Status: In compound commands ([[ ]], [ ], (( ))), the operator yields an exit status of 0 (true) if the condition is met, and 1 (false) if it is not.
  • Expansion Output: When used within arithmetic expansion ($(( ))), the operator does not yield an exit status directly; instead, it expands to the string "1" for true and "0" for false.
Master Bash with Deep Grasping Methodology!Learn More