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

Re: Bash: testing a variable



On Fri, Jul 09, 2021 at 11:40:36PM +0200, john doe wrote:
> On 7/9/2021 9:54 PM, Charles Curley wrote:
> > # if [[ ! ${PATH} =~ .*/home/charles/.local/bin.* ]] ; then
> 
> [[ $PATH =~ \/home\/charles\/... ]] && echo match || echo nomatch
> 
> That is, escaping the backslash.

First, you are escaping forward slashes, *using* backslashes.

Second, you don't need to escape slashes.  They aren't special in an ERE.

Third, you *do* need to escape the dots.  Those *are* special in an ERE.

Fourth, you don't actually need an ERE here.  For a simple substring
match, you can just use [[ $PATH != *substring* ]]

But as I said in a different message, if you're going down this road
(which is more typical for sh than for bash), you will want to delimit
both the left-hand and right-hand sides with extra : characters.

Without those, you will get false positives.  For example, if your PATH
contains /usr/bin and you try to check whether it contains /bin, you'll
get a match, because /bin is literally part of the /usr/bin component.

Anyway, that's why I called that an ugly hack.


Reply to: