There are a few ways to find out if a string contains a substring using bash. Below are a couple of ways this can be done without invoking any other processes.

Star Wildcard

One very simple method is to match strings using the * character to denote any number of other characters. For example:

if [[ "$string" == *"$substring"* ]]; then
    echo "'$string' contains '$substring'";
    echo "'$string' does not contain '$substring'";

If you want to find if a string begins with substring, only use the * character after substring. ie: [[ "$string" == "$substring"* ]]. Similarly, omit the first * character to match strings ending with substring.

Regular Expressions

More advanced pattern matching can be done using regular expressions. In it’s most basic form, we can do simple substring matching like so:

if [[ "$string" =~ $substring ]]; then
    echo "'$string' contains '$substring'";
    echo "'$string' does not contain '$substring'";

This method is computationally slower than the first method, but it can be expanded with other regular expression syntax to do more powerful pattern matching, such as matching strings which contain the substring on word boundaries. See for more details on bash regular expression syntax.

  1. Ranjit says:

    Really good!!

  2. Andrey says:

    Thanks for the info

  3. Mousumi says:

    Helped a lot. Small, nice trick

  4. Dinesh says:

    I do find a issue,where i’m getting data from a variable,which i’m using as string,and comparing a data as substring.when i do with following presets,i’m getting wrong results.

    If possible,kindly help me !!!!!!

  5. Jorge says:

    The last example is wrong, and might be why Dinesh and I were getting wrong results. Please update your example to state that any regex must be unquoted (quoting forces a straight string match where characters [].*+?…etc are taken literatlly.

  6. Tim says:

    Thank you for pointing this out Jorge. This behaviour changed in Bash version 3.2. I’ve updated the post.