| by Arround The Web | No comments

Different Examples of Checking Whether a Sting Contains a Substring or Not

Sometimes, it requires checking whether a particular string exists in another string or not for programming purposes. Since there is no built-in function in Bash to do this task like other programming languages, there are some commands and operators in Bash to do this task. Different ways of checking if a string contains a substring in Bash are shown in this tutorial.

Different Examples of Checking Whether a String Contains a Substring or Not

The different ways of searching a substring in the main string using the “if” statement and “[[“ expression, and the different types of Bash commands are shown in this part of the tutorial.

Example 1: Search the String Using the “If” Statement

Create a Bash file with the following script that checks whether a string exists in another string or not using the “if-else” statement. Here, the values of the main string and substring are taken from the user using the read command. Next, the “if-else” statement checks the substring partially matches with any part of the

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Take the search string value
read -p "Enter the search string: " search

#Check whether the input string exists in the main string or not
if [[ $main_string == *$search* ]]
then
    echo "String exists."
else
    echo "String does not exist."
fi

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring values are “linux” and “Ubuntu”:

Example 2: Search the String Using “[[”

Create a Bash file with the following script that checks whether a string exists in another string or not using the “[[” expression. Here, the values of the main string and substring are taken from the user using the read command like in the previous example. Next, the “[[” expression partially checks the substring matches with any part of the main string using “*”.

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Take the search string value
read -p "Enter the search string: " search

#Check whether the input string exists in the main string or not
[[ $main_string == *$search* ]] && echo "'$search' exists in the string." || echo "'$search' does not exist in the string."

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring values are “come” and “Come”:

Example 3: Search the String Using the “Sed” Command

Create a Bash file with the following script that checks whether a string exists in another string or not using the “sed” command. Here, the value of the main string is taken from the user using the read command. The substring value is used in the regular expression of the “sed” command to check whether the substring exists inside the main string or not.

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Check the input string exists in the main string or not
[ -n "$(sed -n '/Welcome/p' <<< $main_string)" ] && echo "String exists." || echo "String does not exist."

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring value is “Welcome”:

Example 4: Search the String Using the “Grep” Command

Create a Bash file with the following script that checks whether a string exists in another string or not using the “grep” command. Here, the values of the main string and substring are taken from the user using the read command like in the previous example. Next, the substring value is used with the “grep” command to check whether the value exists in the main string or not.

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Take the search string value
read -p "Enter the search string: " search

#Check whether the input string exists in the main string or not
grep -q "$search"  <<< $main_string && echo "'$search' exists." || echo "'$search' does not exist."

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring values are “come” and “go”:

Example 5: Search the String Using the “Awk” Command

Create a Bash file with the following script that checks whether a string exists in another string or not using the “awk” command. Here, the value of the main string is taken from the user using the read command. The substring value is used in the regular expression of the “awk” command to check whether the substring exists inside the main string or not.

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Check whether the input string exists in the main string or not
awk '$0~/to/{print "String exists."}' <<< $main_string

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring value is “to”:

Example 6: Search the String Using the Case Operator

Create a Bash file with the following script that checks whether a string exists in another string or not using the “case” statement. Here, the values of the main string and substring are taken from the user using the read command like in the previous example. Next, the “case” statement partially checks the substring matches with any part of the main string using “*”.

#!/bin/bash
#Assign the main string
main_string="Welcome to linuxhint"
#Print the main string
echo "Main string is: $main_string"
#Take the search string value
read -p "Enter the search string: " search

#Check the input string exists in the main string or not
case $main_string in
    *"$search"*)
    echo "'$search' exists in the string." ;;
    *)
    echo "'$search' does not exist in the string." ;;
esac

Output:

The following output appears after executing the previous script if the main string value is “Welcome to linuxhint” and the substring values are “hint” and “Hint”:

Conclusion

The existence of the substring inside another string can be checked in different ways using a Bash script. The ways of using the different Bash commands and statements to do this task are shown in this tutorial.

Share Button

Source: linuxhint.com

Leave a Reply