shopt -s extglob 的目的是什么

Ham*_*san 25 command-line bash

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

shopt -s extglob
Run Code Online (Sandbox Code Playgroud)

我想知道这个命令到底在做什么,一些后端知识。我还对这个答案添加了评论,但直到现在还没有得到回复。作为 Ubuntu 的新用户,我很想知道这个命令在做什么。

Ser*_*nyy 24

简单来说,globbing 是指模式匹配。Bash 使用简单的 globbing 之类的,echo l*它扩展到当前目录中以字母开头的文件列表l。当然,正如您可以猜到的,它很简单而且很有限。

输入extglob。你可以猜到,它代表extended globbing。此选项允许更高级的模式匹配。来自man bash

extglob If set, the extended pattern matching features described
        above under Pathname Expansion are enabled.
Run Code Online (Sandbox Code Playgroud)

在那之前一点:

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
Run Code Online (Sandbox Code Playgroud)

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

  • 如果您希望它永久存在[您需要将其添加到`~/.bash_profile`(或`~/.bashrc`)中](https://bash.cyberciti.biz/guide/Setting_shell_options#Customizing_Bash_environment_with_shopt_and_set)。关闭终端或退出 shell 时应重置设置的选项。 (2认同)