How to compare strings in Bash
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
String comparison in Bash is simple once you choose the right test syntax. In practice, [[ ... ]] is the safest and most expressive form for equality, pattern matching, and regex checks, while [ ... ] remains common for portable shell-style tests.
Basic Equality and Inequality
For straightforward string equality, both [ ] and [[ ]] work.
The same comparison with [[ ]] looks like this:
For inequality:
In Bash scripts, [[ ]] is usually preferred because it handles many edge cases more cleanly.
Why [[ ]] Is Usually Better
Inside [ ], unquoted variables can cause word splitting or wildcard expansion. That is why quoting is so important there.
With [[ ]], quoting rules are friendlier for plain variable references:
That does not mean quoting is never useful in [[ ]], but it does mean fewer accidental syntax problems.
Pattern Matching With [[ ]]
One major advantage of [[ ]] is shell pattern matching on the right-hand side of ==.
This is not regex. It is shell glob-style matching. If you quote the pattern, it becomes a literal string instead:
That distinction matters.
Regex Matching With =~
Bash also supports regex matching inside [[ ]] with =~.
This is useful when glob patterns are not expressive enough. Keep in mind that regex syntax and shell glob syntax are different tools.
Empty and Non-Empty Strings
For common checks, Bash has dedicated string tests:
These are clearer than comparing directly against an empty literal.
Lexicographic Comparison
If you want alphabetical ordering rather than exact equality, use string comparison operators in [[ ]].
This is lexicographic comparison, not numeric comparison. For numbers, use arithmetic comparisons such as -lt, -gt, or (( ... )).
A Small Reusable Example
Here is a script that shows several forms together:
That gives you exact match, glob match, regex match, and empty-input handling in one place.
Common Pitfalls
The biggest mistake is forgetting to quote variables inside [ ]. Empty values or spaces can break the test expression.
Another issue is confusing glob matching with regex matching. [[ $x == pattern ]] uses shell patterns, while [[ $x =~ regex ]] uses regular expressions.
Developers also compare numbers as strings by accident. [[ "10" < "2" ]] is a string comparison, not a numeric one.
Finally, avoid using == inside [ ] if you care about strict portability to shells beyond Bash. In Bash it works, but = is the more traditional portable form there.
Summary
- Use
[[ ... ]]for most Bash string comparisons because it is safer and more expressive. - Use
==or=for equality and!=for inequality. - Use
[[ $value == pattern ]]for glob-style pattern matching. - Use
[[ $value =~ regex ]]for regex matching. - Quote variables in
[ ... ]tests to avoid shell parsing surprises.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.