BASH Programming

Using the =~ Operator in Bash

The regular expression is a very useful tool to match any content or search and replace the content of a file or in a string using a regex pattern. It can be used with the Bash script in different ways. The =~ symbol is used in the “if” statement of Bash to search any string. Many types of expression can be used to define the matching regex patterns. Some of the commonly used regular expressions and the uses of some expressions with the =~ operator are explained in this tutorial.

Commonly Used Regular Expressions

Expression Purpose
. It is used to search the characters without a newline (\n).
^ It is used to search for characters at the start of the string.
$ It is used to search for characters at the end of the string.
[0-9] It is used to search any number from the range of 0-9 in the string.
[A-Z] It is used to search for any character from the range of A-Z in the string.
[a-z] It is used to search for any character and number from the range of a-z in the string.
[^A-Z0-9] It is used to search all characters except the capital alphabets and digits in the string.
[a-zA-z0-9] It is used to search for any character and number from the range of a-z, A-Z, and 0-9 in the string.
\n It is used to search for the newline character.
\t It is used to search the tab character.

Different Examples of =~ Operators

The different ways of searching a particular string inside a text using the =~ operator and regular expression pattern are shown in this part of the tutorial.

Example 1: Search a Particular String Using the “*” Symbol

Create a Bash file with the following script that takes the main string value where the string is searched and the search string value is searched in the main string value. Next, the “=~” operator is used with the search string to check if the search string exists in the main string or not. Here, the “*” symbol is used to denote any number of characters.

#!/bin/bash

#Take the main string

read -p "Enter the main string value: " strValue

#Take the search string

read -p "Enter the search string value: " search

#Check whether the search string exists in the main string or not

if [[ $strValue =~ .*$search.* ]]; then

echo "The string exists in the text."

else

echo "The string doesn't exist in the text."

fi

The following output appears after executing the script with the main string value of “Learn regular expression” and the search string value of “regular”. Here, the search string exists in the main string:

Example 2: Check the Extension of the Particular File

Create a Bash file with the following script that takes the filename from the command-line argument and check whether the file is a Bash file or not.

#!/bin/bash

#Take the filename from the argument

filename=$1

#Define the searching extension value

extension='bash'

#Check if the extension matches with the file extension or not

if [[ "$filename" =~ \.$extension$ ]]; then

echo "$filename is a bash file."

else

echo "$filename is not a bash file."

fi

The following output appears for the “ping1.bash” filename which is a Bash file:

The following output appears for the “hello.txt” filename which is not a Bash file:

Example 3: Search the Particular Characters in a String

Create a Bash file with the following script that takes a string value and search the range of characters from “a” to “e” in the string.

#!/bin/bash

#Take the main string

read -p "Enter the main string value: " strValue

#Check if the string contains any character from a to e or not

if [[ $strValue =~ [a-e] ]]; then

echo "The string contains characters from 'a' to 'e'"

else

echo "The string does not contain any character from 'a' to 'e'"

fi

The following output appears after executing the script with the “LinuxHint” input value:

The following output appears after executing the script with the “Hello World” input value:

Example 4: Validate the Mobile Number

Create a Bash file with the following script that takes a mobile number of the particular format and check whether the number is valid or invalid using the regular expression pattern and the =~ operator.

#Take a mobile number in the given format

read -p "Enter a mobile number [880-XXXX-XXXXXX]: " mobile

#Set the pattern for matching

regexPattern='^880-[0-9]{4}-[0-9]{6}'

#Check the mobile number is valid or invalid

if [[ $mobile =~ $regexPattern.* ]]; then

echo "Mobile number is valid."

else

echo "Mobile number is Invalid."

fi

The following output appears after executing the script with the “880-1922-032970” input value which is valid:

The following output appears after executing the script with the “880-15677-67345” input value which is invalid:

Conclusion

The methods of using the “=~” operator to search the string values with different types of regular expressions are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.