Skip to main content

How to Check if String Contains a Substring in Bash

In this bash string tutorial, you'll learn to check whether a given string contains a substring or not.

Avimanyu Bandyopadhyay

Warp Terminal

So, you have a long string and you want to check of this string contains a substring in your bash script.

There are more than one way to check for substrings in bash shell. I'll show some simple examples first, followed by a cool bash script that uses this concept in a real-world scenario.

Checking for substring in bash using if else statement

If you are familiar with the conditional statements in bash, you can use it to check if a string contains the substring in the following manner:

if [[ $fullstring == *"$substr"* ]];

Simple example for checking substring

The double brackets are necessary here. I consider this to be the native way because it does not use any other tool. Let me explain the above snippet with proper example.

Initialize a variable with a long string:

avimanyu@linuxhandbook:~$ fullstring="This is a string with a stretch"

Now, store the word “stretch” inside  a variable called substr1:

avimanyu@linuxhandbook:~$ substr="stretch"

Let's make the substring comparison:

avimanyu@linuxhandbook:~$ if [[ $fullstring == *"$substr"* ]]; then

Here, * means zero or more characters. I hope you are familiar with the concept of quotes in bash scripting. The $substr inside the quotes replaces it with its value.

Basically, you are checking if the full string matches xxxxsubstrxxxx type of pattern.

You could also use the substring directly instead of using it through a variable like this:

avimanyu@linuxhandbook:~$ if [[ $fullstring == *"stretch"* ]]; then

You get the gist. Let me complete this script with else statement that shows an alternate message if the substring is not found:


fullstring="This is a string with a stretch"

if [[ $fullstring == *"$substr"* ]]; then
        echo "Found $substr!"
        echo "$substr not found!"

You can easily see that if you run the above shell script, it will find the substring.

avimanyu@linuxhandbook:~$ bash
Found stretch!

A better real world example for finding substring in bash

Substrings can be very helpful in scripts to “interact” with users. Use the read command to let your bash script accept user input.

echo "Hey there..what is your name?"
read Name
echo "Hello "$name"! The Matrix Resurrections trailer is here! Is your pill Red or Blue?"
read Pill
if [[ $Pill == *"Red"* || $Pill == *"red"* ]]; then
  echo " the real world "$Name"!"
  echo "Wake Up "$Name"!"

Give it executable permission and execute the script:

avimanyu@linuxhandbook:~$ chmod +x
avimanyu@linuxhandbook:~$ ./

Output for the above script:

Hey there..what is your name?
Hello avimanyu! The Matrix Resurrections trailer is here! Is your pill Red or Blue?
I have Red pill the real world avimanyu!

Here, I made the if condition slightly better by taking care of two probable chances of case sensitivity. Only Red is the substring here. But red is a possible input too. So, I added another condition. || indicates “or”. If you enter Red, it is a substring. But even though red is not one, I had to add it.

Finding substring in bash using grep command

Here's another way for finding substring in a string in bash. Use the grep command in this manner:

if grep -q "$substr" <<< "$fullstring"; then echo "Found"; fi

The q options means grep will be in quiet mode, not showing any output. Please mind that there are 3 <, not just 2.

I hope this quick tutorial helped you in looking for a specific substring within a string. If you have questions or suggestions, feel free to leave a comment below. Btw..which pill did you choose? Did you make your choice?

Avimanyu Bandyopadhyay
Website @iAvimanyu Facebook Kolkata, West Bengal, India