help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Variables in substitution replacements


From: Chet Ramey
Subject: Re: [Help-bash] Variables in substitution replacements
Date: Thu, 25 Aug 2016 14:52:04 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

On 8/10/16 12:29 AM, Eli Barzilay wrote:
> The following seems like a weird behavior, given the context I doubt
> that it's a bug but it seems good to confirm, just in case.
> 
> I'm trying to replace a character given via a variable by something
> else, and I thought that this should do it:
> 
>     "${str//$r/<>}"
> 
> but it looks like this doesn't work, since ... the contents of $r is
> partially re-interpreted as a pattern??  That's the only explanation I
> have for...

I'll take a closer look at this after bash-4.4 is released.

In some cases, since the variable expansion may contain characters that
are special to pattern matching, the $r may be interpreted as a pattern.
Quoting the $r will suppress that.

In case you're wondering, the outer quotes around the parameter expansion
don't have any effect on pattern matching characters in $r; if you want
them to be treated as literal characters, quote $r.  This is how bash and
ksh93 treat these examples.

Posix changed the handling of parameter expansions containing patterns to
clean up some really ambiguous language, and it supports how bash and ksh93
behave.

http://austingroupbugs.net/view.php?id=221

is the interpretation.  Technically, it only covers the pattern removal
expansions, but it would be really confusing to have patterns treated
differently between expansions.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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