How to use REGEX in Bash shell scripting?

You can always use regex grep or sed or some other external command/programs, but since the version 3 of bash (released in 2004) bash’s provided a built-in regular expression comparison operator "=~".

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], while the first sub-pattern is assigned to BASH_REMATCH[1], BASH_REMATCH[2], etc.

Get hands-on with 1400+ tech skills courses.