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: Eli Barzilay
Subject: Re: [Help-bash] Variables in substitution replacements
Date: Wed, 10 Aug 2016 22:52:51 -0400

Earlier today, Davide Brini wrote:
> On Wed, 10 Aug 2016 00:29:06 -0400, Eli Barzilay <address@hidden> wrote:
> 
> > [...]
> > 
> >     $ t() { echo "${2//$1/<>}"; }
> >     $ t '\' 'foo\*bar'   # ...this not doing anything
> >     foo\*bar
> >     $ t '*' 'foo\*bar'   # ...and this replacing everything
> >     <>
> 
> You should use quotes to prevent expansion, so I think you want this:
> 
> $ t() { echo "${2//"$1"/<>}"; }

Whew...  I tried a pile of things but didn't think to do *that* --
thanks!  I'll at least call Emacs as a supporting witness to my
surprise, in that it mis-highlights that.  (And perhaps should also
not highlight the $1 in my broken version as quoted.)

[I still think that what I expected (and what zsh is doing) is a
better choice as the less surprising option, and I find it weird that
a variable's contents is used as a globbing pattern, and I don't see
how that would be useful while I see the above as asking for bugs...
But given the context of quoting rules of shell scripting that shadows
the whole thing, I'm happy to have any deterministic behavior with an
explanation.]

More practically, I'm guessing that "expanded to produce a pattern" is
the phrase that should explain this -- and if so it could definitely
be improved.  It's especially confusing with the following "just as in
filename expansion" -- I've read that sentence many times yesterday,
and I read it as

  expanded (to produce a pattern) just as in filename expansion

but I think that the correct reading (which I missed, multiple times)
is:

  expanded to produce (a pattern just as in filename expansion)


[Dropping the rest which is obvious now -- thanks again.]

-- 
                   ((x=>x(x))(x=>x(x)))                  Eli Barzilay:
                   http://barzilay.org/                  Maze is Life!



reply via email to

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