当abc*
在 shell 中使用不带引号的模式时,shell 将尝试将其与可用文件名进行匹配(这称为“文件名生成”,但通常称为“globbing”)。如果它无法匹配任何文件名,大多数sh
类似 shell 将不展开模式并将其按原样传递给实用程序。
例子:
$ touch xyz
$ touch abc*
$ tree
.
|-- abc*
`-- xyz
0 directory, 2 files
Run Code Online (Sandbox Code Playgroud)
$ touch xyz*
$ tree
.
|-- abc*
`-- xyz
0 directory, 2 files
Run Code Online (Sandbox Code Playgroud)
该touch xyz*
命令并不会创建一个名为xyz*
,因为文件名xyz
匹配的模式。touch
因此使用xyz
文件名调用该实用程序。
在bash
shell 中,如果 shell glob 不匹配任何内容,则将failglob
shell 选项设置为withshopt -s failglob
将使 shell 抱怨:
$ shopt -s failglob
$ touch 123*
bash: no match: 123*
Run Code Online (Sandbox Code Playgroud)
默认情况下,zsh
shell 中的等效选项处于打开状态。
如果nullglob
在bash
(或NULL_GLOB
in zsh
) 中设置shell 选项将使模式消失,如果它与文件名不匹配:
$ shopt -s nullglob
$ touch fo*
usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]
[-t [[cc]yy]mmddHHMM[.SS]] file ...
Run Code Online (Sandbox Code Playgroud)
(我们得到一个错误,touch
因为它是在没有任何参数的情况下调用的)
为了保证模式被用作字符串(按原样),而不是用于通配符,您应该引用它:
$ touch "file*"
$ touch "file**"
$ touch "file***"
$ tree
.
|-- file*
|-- file**
`-- file***
0 directory, 3 files
Run Code Online (Sandbox Code Playgroud)
在这个例子中不引用文件名只会给你一个被调用的文件file*
(如果目录最初是空的),因为file**
和file***
模式匹配那个file*
名字。
归档时间: |
|
查看次数: |
91 次 |
最近记录: |