How to check if a string contains a substring in Bash
Here are the couple of methods to check if a string contains a substring in bash shell script.
Methods to check if string contains a substring in Bash
Method 1: Use regular expression
#!/bin/bash
str='Techieroop: Solution for DevOps & Open Source Technology'
if [[ $str =~ "DevOps" ]]; then
echo "String is there!"
else
echo "String is not there"
fi
Output:
String is there!
Method 2: Use wildcard (*) to check the string
#!/bin/bash
str='Techieroop: Solution for DevOps & Open Source Technology'
if [[ $str == *"DevOps"* ]]; then
echo "String is there!"
else
echo "String is not there"
fi
Output:
String is there!
(Visited 1,347 times, 6 visits today)