我正在尝试使用参数扩展来转义括号。虽然如果我启用了 extglob
,以下代码不起作用:
#!/usr/bin/env bash
shopt -s extglob
foo='file(2)'
foo=${foo//(/\\(}
foo=${foo//)/\\)}
printf '%s\n' "$foo"
# Expected: file\(2\)
# Actual: file(2\)
当我禁用
file\(2\)
或像这样显式转义左括号时,它会正确输出 extglob
:foo=${foo//\(/\\(}
为什么
extglob
会导致这种情况?我在那里没有看到任何 extglob
模式。此外,右括号没有反斜杠也能正常工作。在 tutorialspoint.com 在线测试,也在本地使用:
GNU bash, version 4.3.30(1)-release (x86_64-unknown-linux-gnu)
GNU bash, version 4.4.18(1)-release (x86_64-unknown-linux-gnu)
GNU bash, version 5.0.0(2)-alpha (x86_64-pc-linux-gnu)
最佳答案
这是由于 bash 中的优化导致的错误。
替换模式时,首先 bash 字符串中的 checks whether the pattern matches anywhere。如果没有,那么进行任何搜索和替换都没有意义。它的方法是根据需要用 *..*
包围它来构造一个新模式:
/* 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 fails to find the closing )
时,它将模式匹配为字符串: 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)
关于bash - ${foo//(/\\(} 不适用于启用 extglobs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51330432/