Bash Extended Globbing 出现语法错误

Pau*_*aul 5 linux debian bash

谁能解释一下:

$ bash
$ shopt -s extglob
$ ls *.(txt|doc)
bash: syntax error near unexpected token `('
$ shopt extglob
extglob         on
Run Code Online (Sandbox Code Playgroud)

这是一个 debian 挤压安装。我期待 extglob 将括号解释为组的开头。

谢谢,

保罗

wom*_*ble 9

因为 extglob 不能那样工作。您必须将修饰符之一放在模式列表的开头((txt|doc)在本例中),如下所示(来自man bash):

          ?(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)

具体来说,ls *.*(txt|doc)产生我猜你想要的行为。