glob 匹配规则

1. 语法

最常用的通配符是*, ?,和[...]

WildcardDescriptionExampleMatchesDoes not match
*匹配任意数量的字符, 包括空Law*Law, Laws, or LawyerGrokLaw, La, or aw
*Law*Law, GrokLaw, or Lawyer.La, or aw
?匹配单个字符?atCat, cat, Bat or batat
[abc]匹配括号内的字符[CB]atCat or Batcat, bat or CBat
[a-z]匹配括号内给的字符范围Letter[0-9]Letter0, Letter1, Letter2 up to Letter9Letters, Letter or Letter10

一般情况下, 路径分隔符(Linux/Unix, MacOS上的/或Windows的\)是从不匹配的,但是一些shell,比如Bash是允许用户规避此问题.

Unix-like

Unix-like 系统, 在[...]上有两个附加含义.

WildcardDescriptionExampleMatchesDoes not match
[!abc]匹配不在括号内的字符[!C]atBat, bat, or catCat
[!a-z]匹配不在括号内给的字符范围Letter[!3-5]Letter1, Letter2, Letter6 up to Letter9 and Letterx etc.Letter3, Letter4, Letter5 or Letterxx

Bash支持以下扩展:

  • **: 递归匹配