Check if a String Contains a Substring in Bash

Check if a String Contains a Substring in Bash

In shell scripting, you may come to a situation where you need to find a substring from a long string.

In this tutorial, you will learn how to write a script that reads a string and finds a substring from it.

Following are different ways via which you can find a substring from a string.

Find Substrings Using the $ Sign

As you may know, the $ sign when used with a variable name returns its value. For example, if the value of variable x is 3, the command echo $x will return 3. You can use the $ sign in the following way to find a substring from within a string.

y=" is a Linux tutorial site"
echo $y
Output is a Linux tutorial site
echo ${y:6:4}

In the above examples, a text string is assigned to the variable y. If you issue the command echo $y, it will return the value contained in the variable y.

But, if you use a curly bracket after $ and mention the position and number of characters, as shown above, you can search a substring from the main string. Here the first number (called offset) specifies the position of the character to start from and the second number specifies the length of the substring.

In the above example, 6 represents the position of the character, to begin with. Starting from 0, the first 5 characters are bytexd, so the substring will start from . and then the remaining 4 characters (.com) would be selected, as can be seen in the output.

The length of the substring is only needed to be specified when the substring lies somewhere between the string. If it lies at the end, or if you want to find the substring from a given position to the end of the string, then there is no need to specify the length of the substring. It is shown in the example below where the length of the substring is not specified.

echo ${p:3}

Here the second option (i.e., the length of the substring) is not specified, so the echo command returned all the remaining characters.

Use A $ Sign To Find a Substring and Then Replace It

You can use the $ sign to not only find a substring from a long string but also to replace that substring with another one. Consider the following example, where a long string is stored in a variable x.

x="In Unix and Windows, Unix is more secure and Unix is open source"
echo ${X/Unix/Linux}
In Linux and Windows, Unix is more secure and Unix is open source.
echo ${X//Unix/Linux}
In Linux and Windows, Linux is more secure and Linux is open source.

Now you use the $ sign and curly brackets to find the substring from the string value stored in x and to specify the text to replace this substring. All of these mentioned elements need to be separated with a forward slash in between.

In the above example, the substring Unix will be searched within the string stored in the variable x and then it will be replaced with a new substring Linux.

You may have noticed in the above example that only the first occurrence of the substring has been replaced. If you want to replace all the occurrences of the substring in the entire long string you have to use double forward slashes (see ${x//Unix/Linux}. Check the last output, where all the occurrences of the word Unix have been found and replaced with Linux.

Find Substrings Using Arrays

You can also find a substring from a string using the array concept. You can store a string in an array and then using array subscripts you can access a particular substring. Check out the following example.

$ array=(this is an example array)
$ echo ${array[0]}
$ echo ${array[1]}
$ echo ${array[3]}
$ echo ${array[2]}

In this example, an array is assigned a text string enclosed in brackets. Now, you can access any of the substrings using the array subscripts, e.g., you want to access the third substring in the string then you have to write array[2]. Remember that array subscripts always start from zero, so the third substring will be represented by 2. Based on this explanation, you can very easily understand the above-mentioned array examples.

Find Substrings Using substr

In the following, you can find a script to extract a substring from the main string using substr. This script requires the user to enter an index value (which specifies the starting point of the substring you want to extract). The index value represents the number of characters from the beginning of the string.

The second input required by the following script is the number of characters you want to extract (means you have to specify the length of the substring).

#! /bin/bash

echo "Enter a String"
read s1

echo "Enter the index of the substring to extract"
read i

echo "Enter the number of characters to extract"
read c

s2='expr substr $s1 $i $c'
echo "The required sub-string is $s2"

The script starts with an echo command asking the user to enter a string. This string will be stored in a variable named s1 using the read command. Next, the user is asked to enter the index value, as explained above. The index value will be stored in another variable named i. Finally, the user is asked to enter the length of the substring to be extracted in the number of characters, the value to be stored in the c variable.

In the next line, you can see an expression written in back quotes. The back quotes are used in scripting for command substitution. In other words, when a shell encounters a backquote, it executes the command written inside and places the result there. It means the command will be executed and its result will be stored in the s2 variable.

The command written inside the backquotes expr substr $s1 $i $c can be interpreted as follows: The expr command will extract a substring of length c characters using substr from whatever string is stored in the variable s1, starting from the index value stored in i.

Finally, the extracted input stored in “s2” variable is displayed back to the user using the echo command. You will get the following output.

Enter a String
Enter the index of the substring to extract
Enter the number of characters to extract
The required sub-string is world

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: Use the string without spaces, otherwise you will get an error message.


In this tutorial, you found several ways to search a substring from a string. There are other methods, such as using a for loop , however we wanted to focus on simple and straightforward methods in this article.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like