conditional-operator

Bash =~ regex match

Matches an extended regular expression. Returns 0 on match, 1 on no match, 2 on invalid regex. Quote literal parts only.

Formal Syntax & Structure

bash syntaxbash
[[ string =~ regex ]]

Return Value

exit status

Matches an extended regular expression. Returns 0 on match, 1 on no match, 2 on invalid regex. Quote literal parts only.

Working Example

Example Snippetbash
id=ID-42regex='^ID-[0-9]+$'if [[ $id =~ $regex ]]; then printf '%s\n' valid; fi
Bash Reference