Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex square close bracket

Tags:

regex

bash

I can use Bash to match a set of characters

$ [[ a =~ [abc] ]]; echo $?
0

However if I want a close square bracket ] to be included in the set, it fails

$ [[ a =~ [abc\]] ]]; echo $?
1

$ [[ a =~ [abc\\]] ]]; echo $?
1
like image 252
Zombo Avatar asked Jan 13 '23 15:01

Zombo


1 Answers

From the man page regex(7):

To include a literal ']' in the list, make it the first character (fol-
lowing a possible '^').  

Testing:

$ [[ "]" =~ []abc] ]]; echo $?
0
like image 96
evil otto Avatar answered Jan 19 '23 12:01

evil otto