Bash can natively match regular expressions in the [[ ]] construct, using the =~ operator. When it matches something, the result is stored in the BASH_REMATCH array variable. BASH_REMATCH contains, in order, the entire matched text, and then each matched subexpression:
$ foo=abcd
$ [[ $foo =~ (.(.))(.) ]]
$ printf "%s\n" "${BASH_REMATCH[@]}"
abc
ab
b
c
Is there something that would look like:
let foo = 'abcd'
let bar = groupmatch(foo, '\(.\(.\)\)\(.\)')
And echo bar would give:
['abc', 'ab', 'b', 'c']
It would be something like split(), but far, far more powerful. The (.(.))(.) is just a toy expression - the full power of regular expressions can be availed in [[ ]].
:h function-listwill list functions grouped by what they are used for – Peter Rincker Mar 24 '16 at 18:57:h functions. – muru Mar 24 '16 at 19:02