⇠ Back to the blog
Cover for How to check if a string contains a substring in Bash

How to check if a string contains a substring in Bash

Christian Nguyen avatarWritten by Christian Nguyen

Substring matching in Bash

Let's say we have the following two strings:

string = ”Cased is the modern engineering enablement platform”

substring = ”Cased”

If we want to check if string contains substring we can use the following expression to do so.

This will evaluate to true and print “Search string contains substring” if string contains substring. Otherwise, it will evaluate to false and print “Search string does not contain substring”.

This expression utilizes Bash's built-in conditional operator == surrounded by the * and [[ ]] wildcards. These wildcards are used for pattern matching. Specifically, the * wildcard represents zero or more characters while the [[ ]] represents a range of characters.

For instance, if you wanted to check if the string starts with or ends with a particular substring it would look like this:

If you're interested and want to learn more schedule a demo or visit our documentation.