so I have this function
function test(){
local output="CMD[hahahhaa]"
if [[ "$output" =~ "/CMD\[.*?\]/" ]]; then
echo "LOOL"
else
echo "$output"
fi;
}
however executing test in command line would output $output instead of "LOOL" despite the fact that the pattern should be matching $output...
what did I do wrong?
Don't use quotes ""
if [[ "$output" =~ ^CMD\[.*?\]$ ]]; then
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With