How do I compare two string variables in an 'if' statement in Bash?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Bash, the usual way to compare two strings in an if statement is with [[ ... ]] and ==. It is readable, safe for most scripting cases, and avoids several quoting problems that happen more easily with the older [ ... ] form.
The short answer looks like this:
But there are a few important details behind that pattern.
The Safest Everyday Form
For plain equality in Bash, prefer:
Why this form is good:
- '
[[ ... ]]is Bash's modern test syntax' - quoting variables keeps spaces intact
- '
==reads naturally for equality'
The older single-bracket syntax also works:
But when you are writing specifically for Bash, [[ ... ]] is usually the better default.
Inequality and Empty Strings
To check inequality:
To check whether a string is empty:
To check whether it is non-empty:
These are common alongside equality checks in real shell scripts.
Quoting Still Matters
Even inside [[ ... ]], quote variable expansions unless you intentionally want pattern behavior.
Good:
Potentially surprising:
If one side contains wildcard characters, the unquoted form can behave like pattern matching rather than literal equality.
Pattern Matching Is a Different Feature
Inside [[ ... ]], the right-hand side of == can be a pattern if unquoted:
That is useful, but it is not the same as comparing two literal strings. If you want literal equality, quote both sides.
Case-Insensitive Comparison
Bash comparisons are case-sensitive by default. If you want case-insensitive comparison, normalize both strings first:
The ${var,,} expansion converts the value to lowercase in Bash.
Lexicographic Comparison
For ordering comparisons, use [[ ... ]] with < and >:
Do not confuse this with numeric comparison. For numbers, use -lt, -gt, and related operators, or Bash arithmetic syntax.
A Small Reusable Example
Here is a script that demonstrates the common cases:
That is a realistic pattern for command-line validation.
Common Pitfalls
The biggest pitfall is forgetting to quote variables and then getting strange behavior from spaces or wildcard characters.
Another common mistake is mixing string and numeric operators. == compares strings. It does not do numeric comparison.
People also forget that [[ "$x" == pattern ]] behaves differently from [[ "$x" == "$pattern" ]] when the right side contains wildcard characters. The first can be pattern matching, the second is literal comparison.
Finally, if you need POSIX sh portability rather than Bash specifically, stick to [ "$a" = "$b" ] instead of Bash-only features.
Summary
- In Bash, the usual string comparison form is
[[ "$a" == "$b" ]]. - Use
!=for inequality and-zor-nfor empty-string checks. - Quote variables unless you intentionally want pattern matching.
- Normalize case when you need case-insensitive comparison.
- Use Bash string operators for strings and numeric operators for numbers.

