How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?
所有的使用shopt -s extglob的答案也提到了shopt -u extglob关闭它。
为什么我想把东西这么有用呢?事实上,为什么不是默认的?
大概它有潜力给一些令人讨厌的惊喜。
他们是什么?
这就是说:这是可能的(虽然不太可能),有人写fo(o)。*实际上意图和括号被视为模式匹配的模式部分的代码。对于bash以不同于POSIX sh规范所要求的方式来解释这个表达式将是破坏兼容性,这是默认情况下在很少的情况下(echo -e与xpg_echo unset是唯一立即到达心神)。
这不同于通常的情况,其中bash扩展是扩展行为未定义的POSIX标准 – 一个基准的POSIX shell通常会抛出一个错误,但bash相反提供一些新的和不同的明确记载的行为 – 因为需要治疗这些字符作为匹配自身由POSIX定义。
引用the relevant part of the specification,重点补充:
An ordinary character is a pattern that shall match itself. It can be any character in the supported character set except for NUL,those special shell characters in 07001 that require quoting,and the following three special pattern characters. Matching shall be based on the bit pattern used for encoding the character,not on the graphic representation of the character. If any character (ordinary,shell special,or pattern special) is quoted,that pattern shall match the character itself. The shell special characters always require quoting.
When unquoted and outside a bracket expression,the following three characters shall have special meaning in the specification of patterns:
?
– A question-mark is a pattern that shall match any character.*
– An asterisk is a pattern that shall match multiple characters,as described in 07002.[
– The open bracket shall introduce a pattern bracket expression.
因此,标准明确地要求除了,*或[或在别处列出的要求引用以匹配自身的任何非NUL字符。 Bash默认情况下关闭extglob的行为允许它在默认配置中符合这个标准。
然而,对于你自己的脚本和你自己的交互式shell,除非你习惯于为包含不寻常模式的POSIX sh编写代码,启用extglob通常是值得的。