[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: umatched pattern in case-statement, why?
From: |
Greg Wooledge |
Subject: |
Re: umatched pattern in case-statement, why? |
Date: |
Mon, 2 Sep 2024 14:07:29 -0400 |
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.
Compare:
hobbit:~$ case a in "?") echo yes;; esac
hobbit:~$ case a in ?) echo yes;; esac
yes
hobbit:~$
Your best bet is to create yet another variable containing the entire
pattern you want to match, and use that (without quotes) as the label:
d='[[:digit:]]'
pattern="($d$d$d) $d$d$d-$d$d$d$d"
line='(001) 345-0000'
case $line in $pattern) echo yes;; esac