+ 是否需要在 find 命令中转义?

Pie*_*tru 3 bash find quoting

我在 bash 中有以下代码,我想知道我是否可以摆脱\+并简单地使用+

   find "$fdir"                                \
      -type f -name "*.org" -o -name "*.texi"  \
      -exec head -n "$n" {} \+                 \
   | grep --color -e ^ -e '^==>.*' 
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 5

是的,+在任何 shell 中都不是特殊字符,不需要引用。^尽管包括 Thompson shell(第一个 UNIX shell)、Bourne shell、fishrceszshextendedglob. {}需要在rc和旧版本中引用 fish*, (, ),>在大多数 shell 中都是特殊的。

请注意,并非所有head实现都接受多个参数。GNU 的实现head确实并且将打印这些==>标题行,但仅在传递多个参数或带有-v选项时才打印。

还要注意 AND(隐含的)优先于 OR ( -o) in find(参见带有多个 `-name` 的 `find` 和 `-exec` 只执行 `-name` 的最后一个匹配项),所以:

find "$fdir"                                       \
   -type f '(' -name '*.org' -o -name '*.texi' ')' \
   -exec head -v -n "$n" '{}' +                    \
   | grep --color -e '^' -e '^==>.*'
Run Code Online (Sandbox Code Playgroud)

(使用'...'引用代替"..."\引用仅用于一致性)。

将在类似 Bourne 的、类似 csh 的 shell 和fish. 在rc/ 中es,您需要删除双引号。在 csh/tcsh 中,您将替换为"$fdir"$fdir:q因此即使$fdir包含换行符也能正常工作。

周围的引号$fdir$n只在类 Bourne 的 shell 中需要,而不是类zshcsh 的 shell。"不是rc/ 中的引用运算符es\rc两者中都不是引用运算符(尽管确实像在其他 shell 中那样进行行延续)。

请参阅如何在 Unix shell 中将特殊字符用作普通字符?哪些字符在哪个 shell 中是特殊的,以及如何转义/引用它们。

  • @jaimet,我现在删除了“是”以避免可能的混乱 (2认同)

归档时间:

查看次数:

53 次

最近记录:

4 年,5 月 前