How to Check if String Contains Substring in Bash

Introduction

In Bash scripting, it's often necessary to check if a string contains a specific substring. This can be useful for validating user input, processing text data, or executing certain actions based on the presence or absence of a certain pattern.

The process of checking if a string contains a substring in bash can be done in several ways, including using the if statement alongside the [[ operator, or using the grep command.

In this article, we will be exploring both methods in detail, along with examples of how to implement them in your scripts. Whether you're a beginner or an experienced bash programmer, this guide will provide a comprehensive overview of how to check if a string contains a substring in bash.

Method 1: Using the if Statement and the [[ Operator

The if statement in Bash is used to perform conditional execution of commands based on the exit status of a test command. In this case, we can use the [[ operator to perform a string comparison between the target string and the substring.

Let's take a quick look at the syntax for using the if statement and the [[ operator to check if a string contains a substring:

if [[ "$string" == *"substring"* ]]
then
  echo "Substring found."
else
  echo "Substring not found."
fi

Here, $string is the target string that you want to check for the presence of a substring, and "substring" is the substring you're searching for. The * wildcard before and after the substring acts as a placeholder for any characters that may come before or after the substring.

Now, we can take a look at the actual example that checks if the string "This is a test string" contains a string "test" as its substring:

# Define the target string
string="This is a test string."

# Check if the string contains the substring "test"
if [[ "$string" == *"test"* ]]
then
  echo "Substring found."
else
  echo "Substring not found."
fi

As expected, the if statement will return "Substring found." as the target string $string contains the substring "test".

Now, let's take a look at the pros and cons of this method for checking if a string contains a substring in Bash.

Pros

  • Syntax is simple and easy to understand
  • Doesn't require an external tool or command
  • Processing time is faster than using the grep command

Cons

  • Limited in functionality, as it only checks for the presence of a substring and cannot be used for more advanced pattern matching

Method 2: Using the grep Command

The grep command is a powerful tool for searching for patterns in text data. It can be used to check if a string contains a substring by using the -q option to perform a quiet search, which returns an exit status based on whether or not the substring was found.

Take a look at the syntax for using the grep command to check if a string contains a substring:

if echo "$string" | grep -q "$substring"
then
  echo "Substring found."
else
  echo "Substring not found."
fi

As in the previous method, $string is the target string that you want to check for the presence of a substring, and $substring is the substring you're searching for. The echo command is used to pipe the target string to the grep command, which performs a quiet search for the specified substring.

Now, we can implement the previous example of checking the existence of the "test" substring inside of the "This is a test string." string:

Define the target string
string="This is a test string."

# Define the substring
substring="test"

# Check if the string contains the substring
if echo "$string" | grep -q "$substring"
then
  echo "Substring found."
else
  echo "Substring not found."
fi

As we expected, the if statement will return "Substring found." as the target string $string definitely contains the substring "test".

Pros

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

  • More powerful and flexible than the if statement and [[ operator
  • Can be used for more advanced pattern matching and text processing tasks
  • Widely used and well-documented

Cons

  • Slower processing time compared to the if statement and [[ operator
  • Requires the use of an external command, which can add complexity to your script

When to Use Each Method

Use the if statement and [[ operator if you need to perform a simple check for the presence of a substring and don't require any advanced pattern matching capabilities. This method is suitable for simple use cases and is easy to understand and implement.

Use the grep command if you need to perform more advanced pattern matching and text processing tasks, or if you prefer a more flexible and powerful tool. This method is suitable for more complex use cases and is a good choice if you're familiar with the grep command.

All-in-all, the choice between the two methods depends on the specific needs of your script and your personal preference. Both methods have their own advantages and disadvantages, and it's up to you to choose the one that best fits your requirements.

Conclusion

In this article, we covered two methods for checking if a string contains a substring in Bash: using the if statement and the [[ operator, and using the grep command.

In conclusion, both methods have their advantages and disadvantages, and it's up to you to choose the one that best fits your requirements. If you're performing a simple check for the presence of a substring, the if statement and the [[ operator is a good choice. If you require more advanced pattern matching capabilities, the grep command is a better option.

In general, it's always a good idea to choose the simplest and most straightforward solution that meets your needs, as it will make your code easier to understand and maintain in the long run. Remember to consider your specific needs and choose the method that best fits your requirements.

Last Updated: April 26th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms