为什么我不会离开在bash启用extglob? [英] Why would I not leave extglob enabled in bash?

查看:173
本文介绍了为什么我不会离开在bash启用extglob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现这里bash的extglob shell选项: -
<一href=\"http://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu\">How我可以使用逆或负通配符时图案在Unix / Linux外壳匹配?

这是使用禁用了javascript -s extglob中也提到禁用了javascript -u extglob将其关闭所有的答案。
我为什么要关掉如此有用的东西吗?事实上,为什么不是在默认情况下?
presumably它有提供一些讨厌的惊喜的潜力。
它们是什么?


解决方案

没有讨厌的惊喜 - 默认关闭行为只是为了与传统的,符合标准的模式语法兼容


这就是说:这是可能的(虽然不太可能),有人写 FO +(O)* 实际上意的的<$ C $。 C> + 和括号被视为其code匹配模式的字面部分。对于bash国米preT以不同的方式比POSIX SH规范要求什么,这个前pression将破坏兼容性,这是对现在默认在极少数情况下完成的(回声-e xpg_echo 取消设置在于立即想到的唯一的一个)。

这是从那里bash的扩展是由POSIX标准的扩展行为未定义通常的情况不同 - 情况基线POSIX外壳通常会抛出一个错误,但代替庆典提供了一些新的和不同明确记录的行为 - 因为需要处理这些字符匹配本身是由POSIX定义

要引用规范的相关部分,与强调说:


  

普通字符是应匹配自身的模式。它可以是在支持的任何字符集,除了 NUL,在引用那些需要引用和以下三种特殊模式字符。匹配应基于用于编码的字符,而不是在字的图形重新presentation的位模式。如果有任何字符(普通,特殊的外壳或图案特)被引用,该模式应匹配字符本身。外壳的特殊字符总是需要引用。


  
  

在加引号和支架前pression外,以下三个字符应模式的规格有特殊意义
  
  


      
  • - ?问号是,将匹配任何字符模式

  •   
  • * - 星号是应匹配多个字符,如模式匹配多个字符

  •   
  • [ - 开放式支架应引入模式支架前pression

  •   

因此​​,该标准明确要求比其他* 或<$ C $任何非NUL字符C> [或者那些为要求报价,以配合自己在其他地方上市。已经默认extglob断击的行为允许它在其默认的配置这个标准一致。


不过,对于你自己的脚本和你自己的交互shell,除非你正在做的POSIX sh的不寻常的图案书面运行code的习惯包括,使 extglob 通常是值得做的事情。

I just found out about the bash extglob shell option here:- How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

All the answers that used 'shopt -s extglob' also mentioned 'shopt -u extglob' to turn it off. Why would I want to turn something so useful off? Indeed why isn't it on by default? Presumably it has the potential for giving some nasty surprises. What are they?

解决方案

No nasty surprises -- default-off behavior is only there for compatibility with traditional, standards-compliant pattern syntax.


Which is to say: It's possible (albeit unlikely) that someone writing fo+(o).* actually intended the + and the parenthesis to be treated as literal parts of the pattern matched by their code. For bash to interpret this expression in a different manner than what the POSIX sh specification calls for would be to break compatibility, which is right now done by default in very few cases (echo -e with xpg_echo unset being the only one that comes immediately to mind).

This is different from the usual case where bash extensions are extending behavior undefined by the POSIX standard -- cases where a baseline POSIX shell would typically throw an error, but bash instead offers some new and different explicitly documented behavior -- because the need to treat these characters as matching themselves is defined by POSIX.

To quote the relevant part of the specification, with emphasis added:

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 Quoting 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 Patterns Matching Multiple Characters.
  • [ - The open bracket shall introduce a pattern bracket expression.

Thus, the standard explicitly requires any non-NUL character other than ?, * or [ or those listed elsewhere as requiring quoting to match themselves. Bash's behavior of having extglob off by default allows it to conform with this standard in its default configuration.


However, for your own scripts and your own interactive shell, unless you're making a habit of running code written for POSIX sh with unusual patterns included, enabling extglob is typically worth doing.

这篇关于为什么我不会离开在bash启用extglob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆