shopt -s extglob 的用途是什么

shopt -s extglob 的用途是什么

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

shopt -s extglob

我想知道这个命令到底起什么作用,一些后端知识。

答案1

简单来说,通配符是指模式匹配。Bash 使用简单的通配符,例如,echo l*它扩展为当前目录中以字母 开头的文件列表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 杂志格雷格的维基

相关内容