Bash Does Not Support RegExps? Read The Fucking Manual!

I have stumbled on this again. There is so many places on the Net (for example Advanced Bash-Scripting Guide) where you can read that Bash does not support regular expression by itself and you have to use other tools (usually grep, sed and awk are mentioned) to match/replace with regex. Since regular expressions are part of POSIX.1-2001, why would Bash not provide such useful function to its users? Of course it provides. You can use conditional expression’s binary operator =~ to match strings with regex. As a result you get a return code 0, 1 or 2 (for matched, not matched and regex syntax error) and an array of matched substrings referenced by the BASH_REMATCH variable. Example:

$ [[ /etc/passwd =~ ^/(.+)/(.+) ]]; echo $? ${BASH_REMATCH[@]}
0 /etc/passwd etc passwd

So if you want to rename a file whose name is stored in a bash variable, you do not have to fork perl or sed for that.