How to check if a string contains a substring in Bash

How to check if a string contains a substring in Bash

If you’re working with Bash and need to check whether a string contains a particular substring, there are several ways to do it. In this article, we’ll walk you through some of the most common methods for checking whether a string contains a substring in Bash.

How to check if a string contains a substring in Bash

By the following methods, you can check if a string contains a substring in bash:

  • Method 1: Using the “grep” command
  • Method 2: Using the “case” statement
  • Method 3: Using the “expr” command

Method 1: Using the “grep” command

The grep command is a powerful tool for searching for patterns in files and directories. It can also be used to search for substrings in a string. Here’s how you can use the grep command to check whether a string contains a substring in Bash:

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

In this example, the “grep” command searches for the substring in the “string” variable. The “-q” option tells “grep” to be quiet and not print anything to the console. Instead, it will exit with a status code of 0 if the substring is found, or a non-zero status code if it is not found. The “if” statement checks the status code and prints a message accordingly.

Method 2: Using the “case” statement

Another way to check whether a string contains a substring in Bash is to use the “case” statement. Here’s an example:

case "$string" in
    *"$substring"*)
        echo "Substring found in string."
        ;;
    *)
        echo "Substring not found in string."
        ;;
esac

In this example, the “case” statement checks whether the “string” variable contains the substring specified by the “substring” variable. The asterisks (*) before and after the substring allow for any characters to appear before or after the substring in the string. If the substring is found, the “echo” command prints a message indicating that the substring was found. If the substring is not found, the “echo” command prints a message indicating that the substring was not found.

Method 3: Using the “expr” command

The “expr” command can also be used to check whether a string contains a substring in Bash. Here’s an example:

if [ $(expr "$string" : ".*$substring") -gt 0 ]; then
    echo "Substring found in string."
else
    echo "Substring not found in string."
fi

In this example, the “expr” command searches for the substring in the “string” variable using a regular expression. The regular expression “.*$substring” matches any characters before the substring, followed by the substring itself. The “expr” command returns the number of characters that matched the regular expression. If the number of characters is greater than 0, then the substring was found in the string. Otherwise, the substring was not found in the string.

Conclusion

In Bash, there are several ways to check whether a string contains a substring. The “grep” command, “case” statement, and “expr” command are all powerful tools that can be used to search for substrings in a string. By using these techniques, you can easily and efficiently search for substrings in your Bash scripts.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *