Bash String Comparison: Find Out IF a Variable Contains a Substring

Posted on in Categories , , , , , , , last updated June 16, 2017

How do I determine whether a variable called spath=”/srv/www/cyberciti.biz/https” contains a substring called “cyberciti.biz”?

You can always find out if a string/word contains another string/word in Linux or Unix shell scripting. For example find out if a word called tips exists in $var=”Here are some tips for you”. There are many ways to test if a string is a substring in bash.
How to find a string is a substring in bash

Method 1: Use case sytnax

You can use the portable BourneShell syntax as follows:

case "$var" in
    *pattern1* ) echo "do something #1";;
    *pattern2* ) echo "do something # 2";;
    * ) echo "Error...";;
esac

Here is a sample code:

#!/bin/bash
spath="/srv/www/cyberciti.biz/https" 
sync_root(){
	echo "Running rsync..."
        rsync -ar $spath/* root@backup.cyberciti.biz:$spath
}
 
case "$spath" in
    *cyberciti.biz*) sync_root ;;
    *) echo "Error: Domain does not exits in path.";;	
esac

Method 2: Bash specific syntax

The following is bash specific syntax and it will not work with BourneShell:

[[ $var = *pattern1* ]]

OR

if [[ $var = *pattern1* ]]
then
    echo "Do something"
fi

Here is a sample code:

#!/bin/bash
# Wrapper for faq pdf generator
# Manually generate pdf files and upload to static nixCraft download server
# --
# Get all defaults and functions 
[[ -f ~/backend/utils/functions.sh ]] && ~/backend/utils/functions.sh
_pdfwriter=${_PYTHON_PDF_WRITER:-~/backend/utils/pdfwriter.beta}
 
[[ $# -eq 0 ]] && { echo "Usage: $0 faq-url"; exit 1; }
 
[[ $1 != *cyberciti.biz/faq/* ]] && { printf "Error: Specify faq url (e.g., http://www.cyberciti.biz/faq/url-1-2-3/)\n"; exit 2; }
 
${_pdfwriter} faq "$1"

Method 3: Bash regex syntax

Bash v3 and above also supports additional regular expressions. The syntax is:

[[ $var =~ .*substring.* ]]

For example find out if a word ‘faq’ exists in $url:

#!/bin/bash
url="https://www.cyberciti.biz/faq/bash-for-loop/"
[[ $url =~ .*faq* ]] && echo "Found" || echo "Not found"
## if syntax ##
if [[ $url =~ .*faq.* ]] 
then
   echo "I found a word faq in ${url}."
else
   echo "Sorry. Not found."
fi

Method 4: AWK regex syntax

The syntax is pretty simple:

# define a var at shell
var="This is a test"
 
# use awk 
awk '$0~/test/{print "A substring called test found"}' <<< $var

Method 5: grep syntax

The syntax is as follows:

var="Hello world"
grep -q 'foo' <<< $var && echo "A substring called foo found" || echo "Not a substring"
grep -q 'world' <<< $var && echo "A substring called world found" || echo "Not a substring"

You can use perl, python and more. See your local bash man page for more information:
man bash
man grep
man awk

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

7 comment

  1. Wow plenty commenting on the bash regex. Well I just wanted to chime in there too. This is sufficient:

    [[ “$MYVAR” =~ cyberciti.biz ]] && echo “Yes it does.”

    It should be noted that the pattern should NOT be quoted, else it is treated as a literal (yoander’s examples are, alas, incorrect)

    I frequently do an arguments check at the start of my scripts to see if we should just print help and exit:

    if [ “$*” =~ –help ]]; then
    printhelp ; exit
    fi

Leave a Comment