Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Check if a String Contains a Substring in Bash - DesignLinux

Nov 21 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

Bash Arrays

Prev Post

How to Install Flask on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version