这是由于 bash 中的优化而导致的错误。
替换模式时,bash 首先检查该模式是否匹配字符串中的任何位置。如果没有,那么进行任何搜索和替换就没有意义了。它的方法是通过*..*
根据需要围绕它构建一个新模式:
/* If the pattern doesn't match anywhere in the string, go ahead and
short-circuit right away. A minor optimization, saves a bunch of
unnecessary calls to strmatch (up to N calls for a string of N
characters) if the match is unsuccessful. To preserve the semantics
of the substring matches below, we make sure that the pattern has
`*' as first and last character, making a new pattern if necessary. */
/* XXX - check this later if I ever implement `**' with special meaning,
since this will potentially result in `**' at the beginning or end */
len = STRLEN (pat);
if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
{
int unescaped_backslash;
char *pp;
p = npat = (char *)xmalloc (len + 3);
p1 = pat;
if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
*p++ = '*';
它尝试与字符串匹配的模式最终成为*(*
现在无意中将开头*(
识别为 extglob 的开头,但是当 bash找不到结尾时)
,它会将模式匹配为字符串:
prest = PATSCAN (p + (*p == L('(')), pe, 0); /* ) */
if (prest == 0)
/* If PREST is 0, we failed to scan a valid pattern. In this
case, we just want to compare the two as strings. */
return (STRCOMPARE (p - 1, pe, s, se));
这意味着除非要在其中进行替换的字符串是字面意思*(*
的,否则优化会无效地拒绝认为无事可做的字符串。当然,这也意味着它自己可以正常工作*(*
:
$ f='*(*'; echo "${f//(/\\(}"
*\(*
如果您要在源代码中伪造此优化检查:
diff --git a/subst.c b/subst.c
index fc00cab0..f063f784 100644
--- a/subst.c
+++ b/subst.c
@@ -4517,8 +4517,6 @@ match_upattern (string, pat, mtype, sp, ep)
c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
if (npat != pat)
free (npat);
- if (c == FNM_NOMATCH)
- return (0);
len = STRLEN (string);
end = string + len;
那么它会在你的情况下正常工作:
$ ./bash -c 'f="my string(1) with (parens)"; echo "${f//(/\\(}"'
my string\(1) with \(parens)