help-bash
[Top][All Lists]
Advanced

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

Re: help with pattern matching needed


From: Christoph Anton Mitterer
Subject: Re: help with pattern matching needed
Date: Tue, 11 Jan 2022 06:18:53 +0100
User-agent: Evolution 3.42.2-1

Hey again.

I'd have one question left:

$ string='foo'
$ bar="*"

When I now do these:
$ echo "${string##*${bar}*}"

$ echo "${string##*"${bar}"*}"
foo
$ echo "${string##*'${bar}'*}"
foo
$ 


So my understanding is that the quotes work the following way:
First, ${bar} is expanded, to * and only then the quotes around that
come into play.


With respect to patterns, is there any difference between double and
single quotes?


I tried for the cases when bar is either " or ' :

$ bar="'"
$ string="fo'o"
$ echo "${string##*${bar}*}"

$ echo "${string##*"${bar}"*}"

$ echo "${string##*'${bar}'*}"
fo'o
$ 
=> I'd have expected the first 2, but the last one... shouldn't that
   be effectively "${string##*'''*}"
   How's that interpreted? An empty string followed by ' ?


$ bar='"'
$ echo "${string##*${bar}*}"
fo'o
$ echo "${string##*"${bar}"*}"
fo'o
$ echo "${string##*'${bar}'*}"
fo'o
$ 

=> 1st and 3rd expected... 2nd, well kinda...


$ string='fo"o'
$ echo "${string##*${bar}*}"

$ echo "${string##*"${bar}"*}"

$ echo "${string##*'${bar}'*}"
fo"o
$ 
=> 1st expected, but 2nd and 3rd are strange... I'd have expected their
   results swapped

At least it didn't seem as if the outer " around the ${} caused any
harm.


Thanks,
Chris.



reply via email to

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