variable

Bash BASH_REMATCH captures

Global indexed array set by regex matching. After success index 0 is full match and later indices are groups; later matches can replace it.

Formal Syntax & Structure

bash syntaxbash
${BASH_REMATCH[index]}

Return Value

behavior

Global indexed array set by regex matching. After success index 0 is full match and later indices are groups; later matches can replace it.

Working Example

Example Snippetbash
id=ID-42regex='^(ID)-([0-9]+)$'if [[ $id =~ $regex ]]; then  printf '%s / %s / %s\n' "${BASH_REMATCH[0]}" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"fi
Bash Reference