bug-bash
[Top][All Lists]
Advanced

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

Re: Bash string substitution bug (?)


From: Dmitry V Golovashkin
Subject: Re: Bash string substitution bug (?)
Date: Sat, 12 Jan 2008 02:32:15 -0500
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

either
       VALUE=host1.blah.com
       echo ${VALUE//.*}
or
    echo ${VALUE/.*}

is accepted by Bash and works exactly as I would expect it - meaning
it correctly produces "host1" - removes the dot (dot is just a character - we are not
in the regular expression world) - so the dot and the trailing domain
name are removed. Bash works fine in both above cases, produces expected output;
${VALUE/.*} naturally seems to be a valid parameter expansion.

Please try once more:

CLUSTER='host5 host1 host2';
VALUE=host1.blah.com;
echo ${CLUSTER//${VALUE%%.*}}

again works perfectly: Outputs  'host5 host2' just as i would expect it.
${VALUE%%.*} similarly to ${VALUE//.*} just removes the first
dot-and-the-trailing domain name.
it is logical to assume and the manual supports it that
    echo ${CLUSTER//${VALUE%%.*}}
is a valid parameter expansion. Again - it works fine.

Therefore it seems to be inconsistent that the nearly identical
expansion
      echo ${CLUSTER//${VALUE/.*}}

is throwing "bad substitution" exception.

To sum things up, consider these two lines (without the "dot"):

echo ${CLUSTER//${VALUE%%*}}
echo ${CLUSTER//${VALUE//*}}

the first works great. the second results in "bash: ${VALUE: bad substitution"

It really does seem remarkably inconsistent: We have parameter expansion
within parameter expansion, one works fine, nearly identical another one
throws an exception... Seems a bug to me.

Thanks!
dm

PS echo ${CLUSTER//${VALUE##.*}}
also works superb - in this case ${VALUE##.*} is empty - because VALUE does not start with a dot. Bash works great here too. It is only when "//" is used - Bash misbehaves...



Bernd Eggink wrote:
Chet Ramey schrieb:
Bernd Eggink wrote:
      prompt: CLUSTER='1 2'; echo ${CLUSTER/${HOSTNAME/.*}}
      output:             -bash: ${HOSTNAME: bad substitution
Apparently bash interprets this as  ${parameter/pattern/string}
where pattern = ${HOSTNAME. Looks like a bug; it works in ksh.
That is, in fact, what is happening.  I'm not sure you can call it
a bug, though -- bash is behaving exactly as documented.

Where exactly is that documented? The only statement I can find in the
documentation referring to this problem is:

"When braces are used, the matching ending brace is the first `}' not
escaped by a backslash or within a quoted string,  and  not
within an embedded arithmetic expansion, command substitution, or
parameter expansion."

I may be wrong, but to me this means that the first opening brace
matches the last closing one, and that the token ${HOSTNAME/.*} is a
correct parameter expansion.

Bernd

--
Bernd Eggink
address@hidden
http://sudrala.de





reply via email to

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