Bash Extended Globbing给出了语法错误

任何人都可以解释一下

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

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

谢谢,

保罗

因为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 

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

您可以使用大括号扩展来执行扩展命令 : ls *.{txt,doc}