How to Use Regex in Bash If Condition? [4 Cases]

A Complete Guide for Beginners Enroll Course Now

Regex or regular expression is a sequence of characters that specifies a search pattern. Matching with regex can be done by incorporating regex with an if statement that involves comparing a string against a pattern determined by the regular expression. In such circumstances, regex is used within the condition of the ‘if statement’. This operation is hugely used in checking the validity of input, parsing text from big strings, extracting necessary data, and searching.

What is Regex?

Regex or Regex matching is an operation to look for similarity between the specified pattern and target string. The pattern is usually a combination of characters and special characters. There are some metacharacters in regex that carry special meaning. Some of these characters are:

  1. ‘*’: Matches one or more occurrences of the preceding character or group.
  2. ‘.’(Dot): Matches any single character.
  3. ‘^’: Anchors the regex at the beginning of the line.
  4. ‘$’: Anchors the regex at the end of the line.

4 Cases of Regex Use in Bash If Condition

Regex can be used to find a specific file extension, match a substring within a string irrespective of case, or use any kind of negated regex to find anything except the original string. In the following section, I will discuss 4 use cases of regex within the ‘if condition’ in bash:

1. Using Regex Inside an If Clause

Regular expression or regex is an approach to accomplish some special task by giving expression-based commands. For example, you can search for a specific type of file using the following syntax: if [[ $file =~ \.(extension)$ ]]. Here the . (dot) matches any single character before the specified extension with dot.

For a practical approach check out the below script to find if a file is with the .txt extension:


if [[ "$filename" =~ \.txt$ ]]; then
echo "Filename has a .txt extension"
echo "Filename does not have a .txt extension"

After assigning the filename to the filename variable, the if [[ "$filename" =~ \.txt$ ]] command looks for a match of the .txt inside the filename variable. Then print a message according to the return status of the if condition.

The .txt file extension found in the filename.The script has found a .txt extension inside the filename.

Note: To find multiple extensions for example .txt and .csv together inside the filename, modify the condition inside the if condition to if [[ $filename =~ \.(txt|csv)$ ]]. Here the txt and the and csv extension are piped together and passed after the . (dot) character.

2. Using Regex in Case-Insensitive Matching

Case-insensitive matching means looking for a match irrespective of the case. First, execute the shopt command with option -s nocasematch to enable case-insensitive matching. Then to perform the matching operation of the pattern inside the variable_name use the syntax: $variable_name =~ ^pattern$ as the condition inside the if statement.

Here is the bash script on how to find case insensitive match:



shopt -s nocasematch

if [[ $name =~ ^susmit$ ]]; then
echo "The name is Susmit (case-insensitive)."
echo "The name is not Susmit (case-insensitive)."

The shopt -s nocasematch command enables case-insensitive search. After that the $name =~ ^susmit$ inside the if condition looks for a match inside the name variable.

Case insensitive match of susmit is found.A case-insensitive match has been found between the assigned pattern and the string inside the name variable.

3. Using a Negated Regex in If Condition

The negated regex matches everything except the original pattern. To achieve this, the syntax used inside the if condition for such regex is: if ! [[ "$string_where_to_search" =~ [original pattern] ]]; For instance, to verify if a string contains any number, inside the string_where_to_search,  the necessary command incorporating appropriate negated regex is: ! [[ "$string" =~ [0-9] ]];.

Here is the complete bash script to check the existence of a number in a string:


string="Hello, world!"

if ! [[ "$string" =~ [0-9] ]]; then
echo "String does not contain any digits"
echo "String contains at least one digit"

After assigning the string variable the if ! [[ "$string" =~ [0-9] ]]; condition checks whether the string variable contains any number. Finally, prints a message according to the result.

No digit is found inside the string using the regex in if condition.The bash script does not find any number or digit in the string.

4. Using Regex with Variables in If Condition

Regex can be saved in a variable and this variable can be used inside the condition of the if clause by following the syntax:  "string" =~ $pattern. Now I will save a pattern inside a variable using variable_name="pattern" and then use this variable inside the condition of an if clause following the syntax: "hello" =~ $variable_name.

Here is the bash script to achieve this:



if [[ "hello" =~ $pattern ]]; then
echo "String matches the regex pattern"
echo "String does not match the regex pattern"

The pattern=”[a-z]+” command assigned a pattern to the pattern variable. After that, the “hello” =~ $pattern condition inside the if clause looks for a match of pattern inside the “hello”. Finally prints a message according to the search result.

Match found between the pattern saved inside the variable and the main string using regex in if condition.See, the script has found a match between the string and the assigned pattern inside the variable.


To conclude, regex is a vital tool for searching the intended pattern within the string utilizing the if condition in Bash. Here in this article, I have covered 4 different cases of using regex in if conditions. If you have any queries or questions regarding this article please comment below. Happy Scripting!

People Also Ask

Why use regex?

The regular expressions (regex) are especially helpful for the filtering purpose. To make a filter more specific or more general, regular expressions are used which are collections of characters that specify a pattern of text to be matched.

What is the use of regex if statement in Bash?

The if statement of the bash script can match text patterns with regex using =~ and double square brackets [[ ]]. To look for patterns that don’t match, you can also utilize negative regexes. Variables can be used to hold regex patterns for usage in if statements. Moreover, regex is an effective tool for pattern matching in Bash because of these qualities.

How to compare two strings in Bash regex?

To compare strings with regular expressions in Bash, use the “grep” command with the “-E” (extended regex) option. Sample script is below:


string="Linux Simply"
if echo "$string" | grep -E "^Linux"; then
echo "Match found"
echo "Match not found"

If the “grep” command searches a string for a pattern and returns a success exit code 0 if the pattern is found and a failure exit code 1 if the pattern is not found.

How do you check if a string is empty in bash?

To check if a string is empty in a Bash script, use the -z conditional which returns 0 if the length of the string is 0 and 1 if it is greater than 0.

Sample script for this purpose is below:


if [[-z "$Var"]]; then
echo "Var is empty"
echo "Var is not empty"

What is a pattern in regular expression?

A regular expression is a pattern that an input text’s regular expression engine looks for matches in. A pattern is made up of one or more structures, operators, or literal characters.

Related Articles

<< Go Back to If Else in Bash | Bash Conditional Statements | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

2 thoughts on “How to Use Regex in Bash If Condition? [4 Cases]”

  1. Hi,
    > point 1 (\. : Here the . (dot) matches any single character )
    No, there \. matches exact character ‘dot’ instead.

    • Hi!
      I guess you missed the explanation part. The “\.” expression will match any character before the extension with the dot.


Leave a Comment