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 context-dependent token that functions either as the variable assignment operator or the string equality test operator. Its behavior is dictated entirely by the presence or absence of surrounding whitespace and the evaluating construct.

1. Variable Assignment (No Whitespace)

When used without surrounding spaces, = binds a value to a variable name. The shell parser identifies a token as an assignment if it consists of a valid identifier immediately followed by =, before any command resolution occurs.
IDENTIFIER=value
Parsing Mechanics:
  • Strict Whitespace Rule: If spaces are placed around the =, Bash interprets the identifier as a command and = as its first argument (e.g., VAR = value attempts to execute a command named VAR with arguments = and value).
  • Right-Hand Side (RHS) Expansion: The RHS undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal.
  • Suppressed Expansions: Crucially, word splitting and pathname expansion (globbing) are not performed on the RHS of a scalar variable assignment.

2. String Equality Comparison (Requires Whitespace)

When surrounded by whitespace within a test construct ([ ], [[ ]], or test), = acts as a binary operator evaluating the lexical equality of two strings.
[ "$string1" = "$string2" ]
[[ "$string1" = "$string2" ]]
Parsing Mechanics:
  • Strict Whitespace Rule: Spaces are mandatory. The [ command and [[ keyword expect distinct arguments. [ "$a"="$b" ] evaluates as a single non-empty string argument, which always returns true (exit status 0) regardless of the variables’ contents.
  • POSIX Standard: = is the POSIX-compliant string equality operator for the test and [ commands. While Bash supports == as a synonym, = is required for strict POSIX portability.
  • Pattern Matching in [[ ]]: Within the Bash-specific [[ ]] extended test construct, if the RHS string is unquoted, = performs pattern matching (globbing) rather than strict literal string comparison. If the RHS is quoted, it forces a literal lexical comparison.
Master Bash with Deep Grasping Methodology!Learn More