Skip to content

Commit 2d9ba84

Browse files
committed
docs(expansion): add globstar
1 parent ef56386 commit 2d9ba84

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎docs/expansion.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ c*.txt
163163

164164
上面例子中,当前目录里面没有`c`开头的文件,导致`c*.txt`会原样输出。
165165

166+
`*`只匹配当前目录,不会匹配子目录。
167+
168+
```bash
169+
# 子目录有一个 a.txt
170+
# 无效的写法
171+
$ ls *.txt
172+
173+
# 有效的写法
174+
$ ls */*.txt
175+
```
176+
177+
上面的例子,文本文件在子目录,`*.txt`不会产生匹配,必须写成`*/*.txt`。有几层子目录,就必须写几层星号。
178+
179+
Bash 4.0 引入了一个参数`globstar`,当该参数打开时,允许`**`匹配零个或多个子目录。因此,`**/*.txt`可以匹配顶层的文本文件和任意深度子目录的文本文件。详细介绍请看后面`shopt`命令的介绍。
180+
166181
## 方括号扩展
167182

168183
方括号扩展的形式是`[...]`,只有文件确实存在的前提下才会扩展。如果文件不存在,就会原样输出。括号之中的任意一个字符。比如,`[aeiou]`可以匹配五个元音字母中的任意一个。
@@ -551,6 +566,8 @@ ls: *.csv: No such file or directory
551566
$ ls */*.txt
552567
```
553568
569+
Bash 4.0 新增了一个`globstar`参数,允许`**`匹配零个或多个子目录,详见后面`shopt`命令的介绍。
570+
554571
**(4)文件名可以使用通配符。**
555572
556573
Bash 允许文件名使用通配符,即文件名包括特殊字符。这时引用文件名,需要把文件名放在单引号里面。
@@ -718,6 +735,37 @@ $ ls /windows/program*
718735
719736
上面例子中,打开`nocaseglob`以后,`program*`就不区分大小写了,可以匹配`ProgramData`等。
720737
738+
**(6)globstar 参数**
739+
740+
`globstar`参数可以使得`**`表示零个或多个子目录。该参数默认是关闭的。
741+
742+
假设有下面的文件结构。
743+
744+
```bash
745+
a.txt
746+
sub1/b.txt
747+
sub1/sub2/c.txt
748+
```
749+
750+
上面的文件结构中,顶层目录、第一级子目录、第二级子目录里面各有一个文本文件。请问怎样才能使用通配符,将它们显示出来?
751+
752+
默认情况下,只能写成下面这样。
753+
754+
```bash
755+
$ ls *.txt */*.txt */*/*.txt
756+
a.txt sub1/b.txt sub1/sub2/c.txt
757+
```
758+
759+
这是因为`*`只匹配当前目录,如果要匹配子目录,只能把它一层层写出来。
760+
761+
打开`globstar`参数以后,`**`就匹配零个或多个子目录。因此,`**/*.txt`就可以得到想要的结果。
762+
763+
```bash
764+
$ shopt -s globstar
765+
$ ls **/*.txt
766+
a.txt sub1/b.txt sub1/sub2/c.txt
767+
```
768+
721769
## 参考链接
722770
723771
- [Think You Understand Wildcards? Think Again](https://medium.com/@leedowthwaite/why-most-people-only-think-they-understand-wildcards-63bb9c2024ab)

‎docs/set.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,25 @@ $ bash -euxo pipefail script.sh
300300
$ shopt
301301
```
302302

303+
`shopt`命令后面跟着参数名,可以查询该参数是否打开。
304+
305+
```bash
306+
$ shopt globstar
307+
globstar off
308+
```
309+
310+
上面例子表示`globstar`参数默认是关闭的。
311+
312+
**(1)-s**
313+
303314
`-s`用来打开某个参数。
304315

305316
```bash
306317
$ shopt -s optionNameHere
307318
```
308319

320+
**(2)-u**
321+
309322
`-u`用来关闭某个参数。
310323

311324
```bash
@@ -318,6 +331,26 @@ $ shopt -u optionNameHere
318331
$ shopt -u histappend
319332
```
320333

334+
**(3)-q**
335+
336+
`-q`的作用也是查询某个参数是否打开,但不是直接输出查询结果,而是通过命令的执行状态(`$?`)表示查询结果。如果状态为`0`,表示该参数打开;如果为`1`,表示该参数关闭。
337+
338+
```bash
339+
$ shopt -q globstar
340+
$ echo $?
341+
1
342+
```
343+
344+
上面命令查询`globstar`参数是否打开。返回状态为`1`,表示该参数是关闭的。
345+
346+
这个用法主要用于脚本,供`if`条件结构使用。
347+
348+
```bash
349+
if shopt -q globstar; then
350+
...
351+
if
352+
```
353+
321354
## 参考链接
322355
323356
- [The Set Builtin](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)

0 commit comments

Comments
 (0)