[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: umatched pattern in case-statement, why?
From: |
Lawrence Velázquez |
Subject: |
Re: umatched pattern in case-statement, why? |
Date: |
Mon, 02 Sep 2024 18:34:42 -0400 |
On Mon, Sep 2, 2024, at 2:07 PM, Greg Wooledge wrote:
> On Mon, Sep 02, 2024 at 17:11:46 +0000, shynur . wrote:
>> Hi, friends~
>>
>> I'm writing some Bash code and having some trouble...
>>
>> d=[[:digit:]] d3=$d$d$d d4=$d3$d
>> line='(001) 345-0000'
>> case $line in
>> "($d3) $d3-$d4")
>> echo $line;;
>> esac
>>
>> Why doesn't the above code print anything? TIA.
>
> The pattern is enclosed in double quotes, so all of the special characters
> in it lose their special meanings.
To be clear, Greg is NOT saying that the "$" characters are being
treated literally, suppressing parameter expansion. The variables
do get expanded, but the quoting causes all resulting characters
to match literally.
This also applies to pattern/regex matching with [[...]]:
$ d='[[:digit:]]'
$ pattern="($d$d$d) $d$d$d-$d$d$d$d"
$ ere="\\($d{3}\\) $d{3}-$d{4}"
$ line='(001) 345-0000'
$ if [[ $line == "$pattern" ]]; then echo match; else echo no match; fi
no match
$ if [[ $line == $pattern ]]; then echo match; else echo no match; fi
match
$ if [[ $line =~ "$ere" ]]; then echo match; else echo no match; fi
no match
$ if [[ $line =~ $ere ]]; then echo match; else echo no match; fi
match
--
vq