${foo//(/\\(} 无法使用启用的 extglobs

${foo//(/\\(} not working with extglobs enabled

我正在尝试使用参数扩展来转义括号。虽然如果我启用了 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\)

当我禁用 extglob 或像这样显式转义左括号时,它会正确输出 file\(2\)

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)

引用搜索字符串可防止将其解释为 glob,从而解决问题:

shopt -s extglob
foo='file(2)'
foo=${foo//'('/'\('}
foo=${foo//')'/'\)'}
printf '%s\n' "$foo"

(引用替换也避免了加倍反斜杠的需要)。

由于 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)