Understanding The $BASH_REMATCH In Bash
$BASH_REMATCH
is a special array variable in the Bash shell that stores the results of matching a regular expression using the =~
operator within a script.
When you use the =~
operator to match a regular expression against a string, any captured groups (i.e., parenthesized subexpressions) are stored in the ${BASH_REMATCH[@]}
array starting from index 1. The entire matched text is stored at index 0.
Here’s an example:
string="Hello World"
pattern='(H[a-z]+)\s*(W[a-z]+)'
if [[ $string =~ $pattern ]]; then
echo "Match found!"
for i in "${!BASH_REMATCH[@]}"; do
echo "$i: ${BASH_REMATCH[$i]}"
done
fi
Output:
Match found!
0: Hello World
1: Hello
2: World
In this example, we define a string and a pattern with two capturing groups. We test if the string matches the pattern using the =~
operator inside an if
statement. If there's a match, we loop through each element of the $BASH_REMATCH
array and print its index and value. As shown in the output, the first capture group ("Hell") is at index 1, while the second capture group ("Wor") is at index 2.
if [[ "abcde" =~ b.d ]]; then
echo "${BASH_REMATCH[0]}" # Outputs "bcd"
fi
In this case, the regex b.d
matches the substring bcd
within abcde
, so ${BASH_REMATCH[0]
will hold this match.
Access To BASH_REMATCH Array Indices
To access the indexes of the BASH_REMATCH
array in bash, you simply refer to the specific index number you're interested in. Each index corresponds to a part of the regular expression match:
${BASH_REMATCH[0]}
: The entire string that was matched.${BASH_REMATCH[1]}
: The first parenthesized subexpression match.${BASH_REMATCH[2]}
: The second parenthesized subexpression match.- And so on…
Here’s an example of how you might access the BASH_REMATCH
indexes:
if [[ "example@domain.com" =~ ([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}) ]]; then
echo "User: ${BASH_REMATCH[1]}" # Prints the username
echo "Domain: ${BASH_REMATCH[2]}" # Prints the domain
fi
In this example, the regex has two capture groups, so ${BASH_REMATCH[1]}
refers to the username part of the email, and ${BASH_REMATCH[2]}
refers to the domain part.
Remember, the array indices start at 0, and the actual content depends on the structure of your regular expression and the input string you are matching against.
Shell Script Tutorial 1
Shell Script Tutorial 2
My YouTube Channel
More shell script videos and linux tutorials on my YouTube Channel.