shopt -s extglob的作用是什么


15

我想从目录中删除除一个以外的所有文件。我在这里找到了解决方案。该解决方案使用命令

shopt -s extglob

我想知道该命令的确切功能,了解一些后端知识。我还对此答案添加了评论,但直到现在都没有得到答复。作为Ubuntu的新用户,我很好奇这个命令在做什么。

Answers:


16

简而言之,通配指的是模式匹配。Bash使用简单的globing之类的方法,将echo l*其扩展到当前目录中以letter开头的文件列表l。当然,您可以猜到,它很简单而且很有限。

输入extglob。如您所料,它代表extended globbing。此选项允许更高级的模式匹配。来自man bash

extglob If set, the extended pattern matching features described
        above under Pathname Expansion are enabled.

在此之前:

If the extglob shell option is enabled using the shopt builtin, several
extended pattern matching operators are recognized.  In  the  following
description, a pattern-list is a list of one or more patterns separated
by a |.  Composite patterns may be formed using  one  or  more  of  the
following sub-patterns:

      ?(pattern-list)
             Matches zero or one occurrence of the given patterns
      *(pattern-list)
             Matches zero or more occurrences of the given patterns
      +(pattern-list)
             Matches one or more occurrences of the given patterns
      @(pattern-list)
             Matches one of the given patterns
      !(pattern-list)
             Matches anything except one of the given patterns

extglob可以使用多种方法。Linux JournalGreg的wiki中提供了很多很好的示例。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.