Regular Expression Matches

$BASH_REMATCH is an array and contains the matched text snippets. ${BASH_REMATCH[0]} contains the complete match. The remaining elements, e.g. ${BASH_REMATCH[1]}, contain the portion which were matched by () subexpressions.

Examples

Print matches :

1
2
3
4
5
text="name=value with spaces"
[[ $text =~ ^([a-z]+)=(.*)$ ]]
echo all: "${BASH_REMATCH[0]}"
echo name: "${BASH_REMATCH[1]}"
echo value: "${BASH_REMATCH[2]}"

Output:

all: name=value with spaces
name: name
value: value with spaces
BashSupport Pro is a Bash IDE with support for $BASH_REMATCH – try it now!
© 2020–2022 Joachim Ansorg
Imprint
Privacy Policy
Legal