How to Check if a Bash String Contains a Substring
Learn how to check if a string contains a substring in Bash scripting. This article provides a step-by-step guide and examples of how to use conditional statements and string manipulation to check for the presence of a substring in a Bash script.
- Published
- Reading time
- 3 min

Introduction
When writing Bash scripts, it is often necessary to check if a string contains a specific substring. This can be useful for performing conditional logic or searching for specific patterns in text. In this article, we will provide a step-by-step guide on how to check if a string contains a substring in Bash scripting. We will cover the different methods available for performing this check and provide examples to demonstrate their use.
Also read: The Ultimate Guide to TACACS
Method 1: Using the Bash [[ ]] Conditional Statement
One way to check if a string contains a substring is by using the Bash [[ ]] conditional statement. This statement allows you to test for the presence of a substring using the syntax ${stringA%stringB}. Here are the steps to use this method:
- Assign the string to a variable: The first step is to assign the string you want to check to a variable. For example, you could use the following command: mystring=”This is a test string”.
- Use the [[ ]] conditional statement: Once you have assigned the string to a variable, you can use the [[ ]] conditional statement to test for the presence of a substring. For example, to check if the string contains the substring “test”, you could use the following command: if [[ $mystring == “test” ]]; then echo “Substring found!”; fi.
- View the results: If the substring is present in the string, the message “Substring found!” will be displayed in the terminal.
Method 2: Using the Bash =~ Operator
Another way to check if a string contains a substring is by using the Bash =~ operator. This operator allows you to match a regular expression against a string. Here are the steps to use this method:
- Assign the string to a variable: The first step is to assign the string you want to check to a variable. For example, you could use the following command: mystring=”This is another test string”.
- Use the =~ operator: Once you have assigned the string to a variable, you can use the =~ operator to match a regular expression against the string. For example, to check if the string contains the substring “test”, you could use the following command: if [[ $mystring =~ “test” ]]; then echo “Substring found!”; fi.
- View the results: If the substring is present in the string, the message “Substring found!” will be displayed in the terminal.
Method 3: Using String Manipulation
A third way to check if a string contains a substring is by using string manipulation. This method involves manipulating the string to check for the presence of a substring. Here are the steps to use this method:
- Assign the string to a variable: The first step is to assign the string you want to check to a variable. For example, you could use the following command: mystring=”Yet another test string”.
- Use string manipulation: Once you have assigned the string to a variable, you can use string manipulation to check for the presence of a substring. For example, to check if the string contains the substring “test”, you could use the following command: if [[ $mystring == “test” ]]; then echo “Substring found!”; fi.
- View the results: If the substring is present in the string, the message “Substring found!” will be displayed in the terminal.
Conclusion
Checking if a string contains a substring is a common task when writing Bash scripts.
Written by
A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.
More from Ajay →$ grep -r "programming" ~/articles
Keep reading

Grep Ignore Case: A Step-by-Step Guide
Introduction Grep is a powerful command-line utility used for searching text files for specific patterns. However, by default, grep is case-sensitive, which…
2 min read

Understanding Grub Edit: A Comprehensive Guide
Introduction Grub (Grand Unified Bootloader) is a popular bootloader used in most Linux systems. It is responsible for loading the operating system into the…
2 min read

An Introduction to Insmod - The Linux Kernel Module Loader
Introduction: Insmod is a command-line utility that is used to load kernel modules into a running Linux kernel. Linux is known for its modular design, and…
3 min read
