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 08:24:29 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Oct 17, 2014 at 11:37:47AM +0200, Jean Delvare wrote:
> I'm am confused by the behavior of the =~ operator with regards to
> quoting, in particular the right hand side:

Quoted things on the right hand side become string constants, NOT
regular expressions.  Same with == or = (they become string constants
instead of glob-style patterns).

> $ [[ a =~ "a" ]] ; echo $?
> 0

You compared the string a and the string a.  They are equal.

> $ [[ a =~ "^a" ]] ; echo $?
> 1

You compared the string a and the string ^a.  They are not equal.

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 ...



reply via email to

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