加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – bash脚本中的[…]代表什么?

发布时间:2020-12-13 23:55:21 所属栏目:Linux 来源:网络整理
导读:我正在阅读 this教程,我遇到了bash脚本使用[…]作为外卡字符.那么究竟什么[…]站在bash脚本中? 解决方法 它是一种正则表达式的字符匹配语法;从 Bash Reference Manual,§3.5.8.1 (Pattern Matching): [...] Matches any one of the enclosed characters. A
我正在阅读 this教程,我遇到了bash脚本使用[…]作为外卡字符.那么究竟什么[…]站在bash脚本中?

解决方法

它是一种正则表达式的字符匹配语法;从 Bash Reference Manual,§3.5.8.1 (Pattern Matching):

[...]
Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a range expression; any character that sorts between those two characters,inclusive,using the current locale’s collating sequence and character set,is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘?’ may be matched by including it as the first or last character in the set. A ‘]’ may be matched by including it as the first character in the set. The sorting order of characters in range expressions is determined by the current locale and the value of the LC_COLLATE shell variable,if set.

For example,in the default C locale,‘[a-dx-z]’ is equivalent to ‘[abcdxyz]’. Many locales sort characters in dictionary order,and in these locales ‘[a-dx-z]’ is typically not equivalent to ‘[abcdxyz]’; it might be equivalent to ‘[aBbCcDdxXyYz]’,for example. To obtain the traditional interpretation of ranges in bracket expressions,you can force the use of the C locale by setting the LC_COLLATE or LC_ALL environment variable to the value ‘C’.

Within ‘[’ and ‘]’,character classes can be specified using the syntax [:class:],where class is one of the following classes defined in the posix standard:

alnum   alpha   ascii   blank   cntrl   digit   graph   lower
          print   punct   space   upper   word    xdigit

A character class matches any character belonging to that class. The word character class matches letters,digits,and the character ‘_’.

Within ‘[’ and ‘]’,an equivalence class can be specified using the syntax [=c=],which matches all characters with the same collation weight (as defined by the current locale) as the character c.

Within ‘[’ and ‘]’,the syntax [.symbol.] matches the collating symbol symbol.

(重点添加到最常见的使用模式)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读