Regex Matching in a Bash if Statement


Regular expressions, commonly known as regex, are invaluable tools for pattern matching and text manipulation. In the Linux environment, specifically within the Bash shell, regex matching plays a vital role in various scripting and automation tasks. In this article, we will explore the concept of regex matching in a Bash `if` statement and its practical applications, highlighting its power and versatility.

The Syntax of Regex Matching in a Bash if Statement

The syntax for employing regex matching within a Bash if statement is as follows:

if [[ $string =~ pattern ]]; then

 # Code to execute if the pattern matches the string

else

 # Code to execute if the pattern does not match the string

fi

Now, let’s delve into the different components of this syntax and understand how they work together:

[[ $string =~ pattern ]]: This conditional expression is the crux of regex matching in Bash. Here, $string represents the variable containing the string to be matched, while `pattern` represents the regex pattern itself.

then: This keyword signifies the beginning of the code block that executes if the pattern successfully matches the string.

else: This optional keyword indicates the start of the code block that executes if the pattern does not match the string. It can be omitted if there is no specific action required for the non-matching scenario.

fi: This keyword signifies the end of the if statement block.

Examples Illustrating Regex Matching in a Bash if Statement

Here are few examples to gain a better understanding of how regex matching can be utilized within a Bash if statement:

Example 1: Matching an Exact String

Suppose we have a variable Phone containing the value LG and we wish to determine whether it matches the string LG exactly:

#!/bin/bash

Phone="LG"

if [[ $Phone =~ ^LG$ ]]; then

 echo " The Phone manufacturer is LG”

else

 echo "The Phone manufacturer is not LG”

fi

In this example, the regex pattern ^LG$ ensures that the entire string matches the word LG precisely. If the pattern matches, the message “The Phone manufacturer is LG” will be displayed; otherwise, the message “The Phone manufacturer is not LG” will be shown:

Example 2: Matching a Pattern with Alternatives

Let’s assume we need to determine whether a filename ends with either “.txt” or “.csv”. In such a case, we can utilize the “|” (pipe) symbol to specify alternative patterns:

#!/bin/bash

file="file.txt"

if [[ $file =~ \.(txt|csv)$ ]]; then

 echo "The file is a text or CSV file."

else

 echo "The file is not a text or CSV file."

fi

Here, the regex pattern \.(txt|csv)$ matches a period followed by either “txt” or “csv” at the end of the string. If the pattern matches, the message “The file is a text or CSV file” will be displayed; otherwise, the message “The file is not a text or CSV file” will be shown.

Example 3: Case-Insensitive Matching

By default, Bash regex matching is case-sensitive. However, the shopt command allows us to perform case-insensitive matching:

#!/bin/bash

nm="Buzdar"
shopt -s nocasematch

if [[ $nm =~ ^buzdar$ ]]; then
    echo "The name is Buzdar (case-insensitive)."
else
    echo "The name is not Buzdar (case-insensitive)."
fi

In this code, we set the variable nm to “Buzdar” and enable case-insensitive matching using the shopt -s nocasematch command. Then, within the if statement, we use the [[ $name =~ ^buzdar$ ]] condition to check if the value of nm matches the regex pattern ^buzdar$. The ^ and `$` symbols ensure that the entire string matches.

Since we enabled case-insensitive matching, the pattern will match “buzdar” regardless of case. If the pattern matches, the code block within the if statement executes and outputs “The name is Buzdar (case-insensitive).” If the pattern doesn’t match, the code block within the else statement executes and outputs “The name is not Buzdar (case-insensitive)”.

Note: It is essential to note that the =~ operator used in the Bash if statement is specific to the Bash shell and may not be available in other shells. Additionally, the syntax of regex patterns might vary slightly depending on the version of Bash being used.

Conclusion

Regex matching within a Bash if statement is an indispensable tool for efficient text manipulation and pattern matching in the Linux environment. By mastering this technique, developers can unlock a world of possibilities in scripting and automation, paving the way for enhanced productivity and streamlined workflows.

Print Friendly, PDF & Email
Categories