In Bash scripting, a host of operators is available to manipulate, compare, and test data. One of the operators is the =~. Though not very frequently used, it is an extremely powerful operator that enables us to match string patterns using regular expressions. This article goes into the practical application of the =~ operator in Bash.
Understanding the =~ Operator
The =~ operator is unique to Bash, and used in conditional expressions. This operator will be used normally to compare a string with a given pattern in regular expressions. Whether the string matches with the pattern, it displays 0 for true, otherwise, 1 as the result.
Syntax of the =~
Operator
The syntax is straightforward. The =~ operator is used within [[ and ]], and the string and the regular expression are the operands, as demonstrated below:
[[ string =~ regular_expression ]]
If the string matches the regular expression, the exit status of the operation is 0; otherwise, it’s 1.
Practical Application of the =~ Operator
Now let’s delve into some practical examples to see how the `=~` operator can be applied.
Example 1: Simple Pattern Matching
Let’s start with a basic example. We have a string that says “Welcome to Bash scripting,” and we want to see if the string contains the word “Bash.”
#!/bin/bash
str="Welcome to Bash scripting"
if [[ $str =~ Bash ]]; then
echo "The string contains the word Bash."
else
echo "The string does not contain the word Bash."
fi
When we run this script, it will output “The string contains the word Bash.”
Example 2: Using Regular Expressions
One of the significant advantages of the `=~` operator is that it allows for regular expression pattern matching. Suppose we want to check if a string contains any digit. Here’s how we can do it:
#!/bin/bash
str="Order 5 pizzas"
if [[ $str =~ [0-9]+ ]]; then
echo "The string contains a digit."
else
echo "The string does not contain a digit."
fi
The `[[ $str =~ [0-9]+ ]]
` expression will check for one or more occurrences of any digit (0-9) in the string. The script will print “The string contains a digit.”
Example 3: Extracting Matches
The =~ operator can also be used to extract matches. When a match is found, the BASH_REMATCH array variable is populated with the portion of the string that matched and any captured group matches.
Let’s assume we have a date string, and we want to extract the day, month, and year:
#!/bin/bash
date="23-05-2023"
regex="([0-9]{2})-([0-9]{2})-([0-9]{4})"
if [[ $date =~ $regex ]]; then
day=${BASH_REMATCH[1]}
month=${BASH_REMATCH[2]}
year=${BASH_REMATCH[3]}
echo "Day: $day, Month: $month, Year: $year"
fi
In this script, the regular expression ([0-9]{2})-([0-9]{2})-([0-9]{4}
) will match a date format of dd-mm-yyyy, and the BASH_REMATCH array will hold the matched groups. The script will print “Day: 23, Month: 05, Year: 2023”.
Conclusion
The =~ operator is the holy grail for Bash scripters. Mastering it and using its power can enable even the most complex string pattern matching and regular expression logic within your scripts. Just remember to use it within double brackets [[ ]]
and always test your scripts to make sure your regular expressions are working as expected. Happy scripting!