Don't use quotes ""
if [[ "$output" =~ ^CMD\[.*?\]$ ]]; then
Update : (in response to @frhd)
Well, the regex operator =~
expects an unquoted regular expression on its RHS and does only a sub-string match unless the anchors ^
(start of input) and $
(end of input) are also used to make it match the whole of the LHS.
Quotations ""
override this behaviour and force a simple string match instead i.e. the matcher starts looking for all these characters \[.*?\]
literally.