Reputation: 2010
Alright folks. I am writing a bash script and would like compare a string against a regular expression containing a #
using the =~
operator.
Here is what I have so far:
if [[ ${line} =~ \s*\# ]]; then
#do things
fi
As you can see, I am attempting to escape the #
with a \
, which is supposed to be possible according to this article. This is really confounding to me, however. My syntax highlighter is still highlighting the text following the #
as if it were a comment.
Is my syntax highlighter incorrect? Will the escape of the #
interfere with the parsing of the regex? Is there some way I can use quotes to avoid this issue?
Upvotes: 2
Views: 169
Reputation: 784998
You can do:
re='\s*#'
if [[ $line =~ $re ]]; then
#do things
fi
Upvotes: 5