Bash scripting
String comparison
Programming
Linux command line
Shell scripts

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.

Browse interview questions

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.

bash
1name="alice"
2
3if [ "$name" = "alice" ]; then
4  echo "equal"
5fi

The same comparison with [[ ]] looks like this:

bash
1name="alice"
2
3if [[ $name == "alice" ]]; then
4  echo "equal"
5fi

For inequality:

bash
if [[ $name != "bob" ]]; then
  echo "not bob"
fi

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.

bash
1value="hello world"
2
3if [ "$value" = "hello world" ]; then
4  echo "match"
5fi

With [[ ]], quoting rules are friendlier for plain variable references:

bash
1value="hello world"
2
3if [[ $value == "hello world" ]]; then
4  echo "match"
5fi

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 ==.

bash
1file="report-2026.txt"
2
3if [[ $file == report-*.txt ]]; then
4  echo "matched pattern"
5fi

This is not regex. It is shell glob-style matching. If you quote the pattern, it becomes a literal string instead:

bash
if [[ $file == "report-*.txt" ]]; then
  echo "literal comparison"
fi

That distinction matters.

Regex Matching With =~

Bash also supports regex matching inside [[ ]] with =~.

bash
1value="abc123"
2
3if [[ $value =~ [0-9]+ ]]; then
4  echo "contains digits"
5fi

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:

bash
1value=""
2
3if [[ -z $value ]]; then
4  echo "empty"
5fi
6
7if [[ -n "text" ]]; then
8  echo "non-empty"
9fi

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 [[ ]].

bash
1left="apple"
2right="banana"
3
4if [[ $left < $right ]]; then
5  echo "apple comes first"
6fi

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:

bash
1#!/usr/bin/env bash
2
3input="$1"
4
5if [[ -z $input ]]; then
6  echo "no input"
7elif [[ $input == admin ]]; then
8  echo "exact match"
9elif [[ $input == user-* ]]; then
10  echo "pattern match"
11elif [[ $input =~ ^[a-z]+[0-9]+$ ]]; then
12  echo "regex match"
13else
14  echo "no match"
15fi

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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.