0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check if String Contains Substring

Use the syntax if [[ "$string" == *"$substring"* ]]; then to check if $string contains $substring in Bash.
📋

Examples

Inputstring='hello world'; substring='world'
OutputSubstring found
Inputstring='bash scripting'; substring='script'
OutputSubstring found
Inputstring='openai'; substring='chat'
OutputSubstring not found
🧠

How to Think About It

To check if one string contains another in Bash, think of it like looking for a word inside a sentence. You use a condition that tests if the main string has the smaller string anywhere inside it by using pattern matching with * wildcards around the substring.
📐

Algorithm

1
Get the main string and the substring to check.
2
Use a conditional statement to test if the main string matches a pattern containing the substring.
3
If it matches, print or return that the substring is found.
4
Otherwise, print or return that the substring is not found.
💻

Code

bash
#!/bin/bash
string="hello world"
substring="world"

if [[ "$string" == *"$substring"* ]]; then
  echo "Substring found"
else
  echo "Substring not found"
fi
Output
Substring found
🔍

Dry Run

Let's trace the example where string='hello world' and substring='world' through the code

1

Set variables

string='hello world', substring='world'

2

Check condition

Does 'hello world' match pattern '*world*'? Yes

3

Print result

Output: 'Substring found'

StepStringSubstringCondition ResultOutput
1hello worldworldN/AN/A
2hello worldworldTrueN/A
3hello worldworldTrueSubstring found
💡

Why This Works

Step 1: Pattern Matching

The [[ "$string" == *"$substring"* ]] uses wildcards * to check if $substring appears anywhere inside $string.

Step 2: Conditional Execution

If the pattern matches, the if condition is true, so the script prints 'Substring found'.

Step 3: Else Case

If the substring is not found, the condition is false, and the script prints 'Substring not found'.

🔄

Alternative Approaches

Using grep
bash
#!/bin/bash
string="hello world"
substring="world"
echo "$string" | grep -q "$substring"
if [ $? -eq 0 ]; then
  echo "Substring found"
else
  echo "Substring not found"
fi
Uses external grep command; slightly slower but works in older shells without [[ ]] support.
Using case statement
bash
#!/bin/bash
string="hello world"
substring="world"
case "$string" in
  *"$substring"*) echo "Substring found" ;; 
  *) echo "Substring not found" ;; 
 esac
Uses shell pattern matching with case; clean and readable alternative.

Complexity: O(n) time, O(1) space

Time Complexity

The check scans the main string once to find the substring, so it runs in linear time relative to the string length.

Space Complexity

No extra memory is needed besides the input strings; the check is done in place.

Which Approach is Fastest?

The built-in [[ ]] pattern matching is fastest and simplest; grep involves spawning a process, which is slower.

ApproachTimeSpaceBest For
[[ ]] with wildcardsO(n)O(1)Simple, fast substring check in modern Bash
grep commandO(n)O(1)Compatibility with older shells, supports regex
case statementO(n)O(1)Readable pattern matching alternative
💡
Use double square brackets [[ ]] with wildcards * around the substring for a simple and fast check.
⚠️
Forgetting to quote variables or wildcards, which can cause syntax errors or wrong matches.