• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Check if a String Contains a Substring in Bash

Written by Admin, Updated On November 21, 2020
bash, terminal
Check if a String Contains a Substring in Bash

While you working with string in Bash need to check whether a string contains another string. In this guide, we will show you multiple ways to check if a string contains a substring in bash scripting.

Use Wildcards#

It is easy to use asterisk wildcard symbols (asterisk) * to compare with the string. Wildcard is a symbol used to represent zero, one or more characters. If the string contained the string it will returns true.

Let’s see an example, we are using if statement with equality operator (==) to check whether the substring SUBSTR is found within the string STR:

#!/bin/bash

STR='Ubuntu is a Linux OS'
SUBSTR='Linux'
if [[ "$STR" == *"$SUBSTR"* ]]; then
  echo "String is there."
fi

It will show following output:

String is there.

Use the case operator#

You can use the case statement instead of if statement to check whether a string includes or not to another string.

#!/bin/bash

STR='Ubuntu is a Linux OS'
SUBSTR='Linux'

case $STR in

  *"$SUBSTR"*)
    echo -n "String is there."
    ;;
esac

Using Regex Operator#

Another way is to use the regex operator =~ to check whether a specified substring occurs within a string. When this operator is used, the right string is considered as a regular expression.

The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.

#!/bin/bash

STR='Ubuntu is a Linux OS'
SUBSTR='Linux'

if [[ "$STR" =~ .*"$SUBSTR".* ]]; then
  echo "String is there"
fi

The script will echo the following:

String is there

Using Grep#

The grep command can also be used to find strings in another string.

In the below example, we are giving string in the $STR variable to grep and check if the string $SUBSTR is found within the string. It will return true or false.

#!/bin/bash

STR='Ubuntu is a Linux OS'
SUBSTR='Linux'

if grep -q "$SUB" <<< "$STR"; then
  echo "String is there"
fi

Conclusion#

In this article, you learned how to check a substring contains in the given string using multiple ways in Bash script.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash Arrays
Next Article   How to Increment and Decrement Variable in Bash

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook