help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Confused by =~ behavior with regards to quoting


From: Greg Wooledge
Subject: Re: [Help-bash] Confused by =~ behavior with regards to quoting
Date: Fri, 17 Oct 2014 09:44:04 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Oct 17, 2014 at 03:34:53PM +0200, Jean Delvare wrote:
> On Fri, 17 Oct 2014 08:24:29 -0400, Greg Wooledge wrote:
> > The recommendation for people who want to use =~ is to put the regular
> > expression into a shell variable, and then use that variable (unquoted)
> > on the right hand side.
> > 
> > re='^gibberish$'
> > if [[ $foo =~ $re ]]; then ...
> 
> Thanks for the suggestion, I'll remember if I ever need it. That being
> said, I can't really think, off the top of my head, of a case where
> using the regular expression directly would cause problems. My current
> code looks like:
> 
> [[ "$1" =~ ^[0-9]+$ ]]
> 
> (succeeds if $1 is a positive integer) and it works just fine as far as
> I can tell.

Yes, simple cases like this work fine without putting the regular
expression in a variable.  But then again, simple cases like this don't
need the =~ operator at all.

[[ $1 = +([0-9]) ]]

And you don't even have to enable extglob here -- for some reason,
even with extglob disabled, this particular case *still* works.



reply via email to

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