Bash Regular Expressions - Mitch Frazier - Linux Journal
Bash Regular Expressions - Mitch Frazier - Linux Journal
Bash Regular Expressions - Mitch Frazier - Linux Journal
When working with regular expressions in a shell script the norm is to use grep or sed or some
other external command/program. Since version 3 of bash (released in 2004) there is another
option: bash's built-in regular expression comparison operator "=~".
Bash's regular expression comparison operator takes a string on the left and an extended regular
expression on the right. It returns 0 (success) if the regular expression matches the string,
otherwise it returns 1 (failure).
In addition to doing simple matching, bash regular expressions support sub-patterns surrounded
by parenthesis for capturing parts of the match. The matches are assigned to an array variable
BASH_REMATCH. The entire match is assigned to BASH_REMATCH[0], the first sub-pattern is
assigned to BASH_REMATCH[1], etc..
The following example script takes a regular expression as its first argument and one or more
strings to match against. It then cycles through the strings and outputs the results of the match
process:
#!/bin.bash
while [[ $1 ]]
do
if [[ $1 =~ $regex ]]; then
echo "$1 matches"
i=1
n=${#BASH_REMATCH[*]}
while [[ $i -lt $n ]]
do
echo " capture[$i]: ${BASH_REMATCH[$i]}"
let i++
done
else
echo "$1 does not match"
fi
shift
done
Assuming the script is saved in "bashre.sh", the following sample shows its output:
https://www.linuxjournal.com/content/bash-regular-expressions 1/2
05/04/2020 Bash Regular Expressions | Linux Journal
aabbxcc matches
capture[1]: bbx
aabbcc does not match
https://www.linuxjournal.com/content/bash-regular-expressions 2/2