help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Variables in substitution replacements


From: Eli Barzilay
Subject: [Help-bash] Variables in substitution replacements
Date: Wed, 10 Aug 2016 00:29:06 -0400

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...

    $ t() { echo "${2//$1/<>}"; }
    $ t '\' 'foo\*bar'   # ...this not doing anything
    foo\*bar
    $ t '*' 'foo\*bar'   # ...and this replacing everything
    <>

but if it *is* re-interpreted as a pattern then I don't see how this
happens:

    $ t '"*"' 'foo\*bar'
    foo\*bar

since I'd expect the quoted quotes to be part of that pattern; and there
are also these:

    $ t '"*"' '"foo\*bar"'
    <>
    $ t '"*"' 'f"oo\*ba"r'
    f<>r

which I can't explain.

Looking at the code, it looks like the match is done with strmatch() and
it looks like the pattern argument to that should be an actual pattern
as in the pattern after it was pulled out of whatever quotes it was
under.  And it looks like (note triple-looks-like, so these are guesses)
something is wrong because when I use something like

    ls \"*\"

then strmatch's pattern is exactly these 5 chars (bslash-dquote-star-
bslash-dquote), but in the last example above the pattern is just the
dquote-star-dquote which is not something that can be done with file
patterns (since that would always add the bslashes?).

So all of this feels like a bug, since regardless of what it's doing,
the end result is that I start with a variable holding a quoted * which
eventually gets interpreted as a glob star.

As a side note, zsh does what I expected:

    > t() { echo "${2//$1/<>}"; }
    > t '\' 'foo\*bar'
    foo<>*bar
    > t '*' 'foo\*bar'
    foo\<>bar

-- 
                   ((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]