There are many situations where you might want to check if a string contains a specific substring in a Bash script. For example, you might want to check if a command-line argument passed to a script is a specific value or if a variable contains a certain substring.
So, this article will guide you through all the different methods to check if a string contains a substring in bash or not:
We’ll explain each of the above in detail so let’s get started!
Method 1: Using the Bash Wildcard Expressions
In Linux Shell, the bash wildcard is a set of special characters to find the text in a file or string. The three main characters include
- Asterisk
- Square brackets or [ ] (to match the number of characters within a given range)
- A question mark or? (to match a single character)
We’ve created this shell script to check whether a string contains a substring (we’ve used the asterisk, and you can change the symbol as per the requirements).
#!/bin/bash
STRING='Linux is a highly customizable operating system
SUBSTRING='highly'
if [[ ''$STRING'' == *''$SUBSTRING''* ]]; then
echo ''The substring you searched, is there''
fi
In the above script, we’ve created a variable named ‘STRING’ with a string that will be searched. Just below that, there’s another variable named ‘SUBSTRING,’ which has the substring we’re going to search. In the ‘if’ statement, we’ve accessed the ‘STRING’ variable and then compared it to the SUBSTRING, which is surrounded by ‘*’ to search for a character.
To execute this script in Linux Terminal, we’d use this format.
$ sudo bash <File-Name.sh>
The script created above is named ‘test’, and ‘sh’ is the extension for the script, and its output can be seen below:
$ sudo bash test.sh
As expected, we can see that it displays “The substring you searched is there,” which indicates the successful execution of our script, and we’ve checked if the string we added has a substring.
Method 2: Using the case Operator
The case statement in bash works the same as the switch statement in Java, C++, or JavaScript programming languages. It is used extensively to simplify the code by simplifying complex conditional statements. To simplify the above syntax here’s an example where we’d check if a string has the required substring, but first, you need to understand the script.
#!/bin/bash
STRING='For Linux related tutorials, itslinuxfoss.com is best'
SUBSTRING='Linux'
case $STRING in
*"$SUBSTRING"*)
echo "Matching substring found"
;;
esac
The above script is named ‘test.sh’, and we stored a string inside the variable named ‘STRING’. Now the substring that we need to find is stored inside the variable named ‘SUBSTRING’, and after that, the case statement starts where we’ve checked if our string has the substring we’re looking for.
To execute the above script, we’d use this command.
$ sudo bash test.sh
The above script would display nothing if there weren’t any matching substring, which in this case, was there.
Method 3: Using the Regular Expression
The Regex operator ‘=~’ is also used to check a string for a substring, and here’s the format for using it.
#!/bin/bash
STRING='A string of characters
SUBSTRING = 'Substring to search'
if [[ $VAR =~ *“SUBSTRING”* ]]; then
echo "Any Message”
fi
To understand the above syntax, here’s an example, but first, let’s go through the script.
The above image shows a script to match the characters found within a string, and it’ll display “it’s there” if there’s any match for the ‘th’. Now, let’s execute this script.
$ sudo bash test.sh
Method 4: Using the grep Command
Checking a string for a substring is one of the many uses of the grep command, and here’s the syntax of it.
#!/bin/bash
STRING='A string of characters
SUBSTRING='substring to check'
if grep "$SUB" <<< "$STR"; then
echo "Message to display"
fi
The syntax mentioned above is as simple as it gets, and you’d need to define a string of characters in the ‘STRING’ variable while the ‘SUBSTRING’ is where you store the substring to check.
Here’s an example (script) to help you understand better.
The above script has two variables that have a string of characters stored in them, and using the grep command; we’ve checked if our string has a substring in it or not. Now, let’s execute this script.
$ sudo bash test.sh
Using all the above methods, you can check whether the string has a substring.
Conclusion
It is important to check if a string contains a substring in Bash because this operation allows you to perform different actions based on whether the string contains the substring. You can check if the string contains a substring or not in bash “using Wildcard Expressions”, “using the case operator”, “using Regular expressions (regex)”, and “using the grep Command”.