help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash string RE pattern matching


From: Greg Wooledge
Subject: Re: bash string RE pattern matching
Date: Wed, 26 Jul 2023 20:57:35 -0400

On Wed, Jul 26, 2023 at 07:13:38PM -0400, Lee wrote:
> Why doesn't =~ match strings inside of double quotes?
> 
> $ bash bashre
> os = "Linux"   re = "^Linux"
> "$os" =~ ${re} is true
> "$os" =~ "${re}" is False

Because any quoted character within the RE is taken literally, instead
of having a special regular expression meaning.

"^" acts exactly like \^ because they are both ways of quoting the
character.

This is true even if the ^ is part of an expansion, instead of written
literally within the [[ ]] command.

If you put your RE in a variable, and you want it to be matched as an RE
instead of as a string, you must not use quotes around the variable
expansion.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]