个人工具
登录
查看“UbuntuHelp:ShellGlobbing”的源代码 - Ubuntu中文
UbuntuHelp
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“UbuntuHelp:ShellGlobbing”的源代码
来自Ubuntu中文
←
UbuntuHelp:ShellGlobbing
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{{From|https://help.ubuntu.com/community/ShellGlobbing}} {{Languages|UbuntuHelp:ShellGlobbing}} === Introduction === One of the best features of the shell is globbing. Globbing is the equivalent of regular expressions of files and directories. As everything else, it's probably best explained with an example: <pre><nowiki> $ ls code devel downloads Pictures src cat.py database.py gravity.py insert.py oldmaid.py $ ls *.py cat.py database.py gravity.py insert.py oldmaid.py </nowiki></pre> === The Basics === In the shell, the * character means "match everything". This is the most common globbing character. So, globbing can be defined has a set of character that are expanded by the shell. Note, that the globbing characters are expanded before executing a command. For example: <pre><nowiki> $ ls -F bar/ baz/ foo -rf tata tintin $ rm * $ ls -rf </nowiki></pre> Here, you see that the directory contains two sub-directories, 'foo' and 'bar'. The directory also contains a file named '-rf'. The '*' in the second command, 'rm', is expanded to: <pre><nowiki> $ rm bar baz foo -rf tata tintin </nowiki></pre> Now, you probably know about the '-rf' options of 'rm'. They make 'rm' delete all the directories and their contents. So, why the file '-rf' is still present after the command as been executed? If you look carefully the '-rf' file was never given to 'rm', because 'rm' treated '-rf' as an option. Alternatively, if you had given the '--' option, which make most commands stop processing options, the result would had been different: <pre><nowiki> $ ls -F bar/ baz/ foo -rf tata tintin $ rm -- * rm: cannot remove `bar': Is a directory rm: cannot remove `baz': Is a directory $ ls bar baz </nowiki></pre> === Globbing characters === There is other characters other than *. Here a list of the globbing characters, that are available in most shells: * <code><nowiki>?</nowiki></code>: matches any single character, regardless what the character is. <pre><nowiki> $ ls bar baz bzr $ ls b?r bar bzr</nowiki></pre> * <code><nowiki>[...]</nowiki></code>: matches any character inside the brackets. The characters can be either be a range of character or the character itself. For example, <code><nowiki>[a-z]</nowiki></code> would matches all the lowercase letters through 'a' to 'z'. <pre><nowiki> $ ls bar foo FOO $ ls [A-Z] FOO</nowiki></pre> * <code><nowiki>[!...]</nowiki></code>: matches all the characters that are not in the backrets. This is analogous to the caret (^) character used in regular expressions. <pre><nowiki> $ ls bar foo FOO $ ls [!A-Z] bar foo</nowiki></pre> * <code><nowiki> {p1,p2,...} </nowiki></code>: matches all the patterns described in the brackets. They can be any of above globbing patterns or specific characters. <pre><nowiki> $ ls bar bar2 bar3 baz foo1 foo2 foo3 $ ls {bar,foo[0-9]} bar foo1 foo2 foo3</nowiki></pre> === Conclusion === There's many other globbing characters, but they are often specific to certain shells. I encourage you to read the section in the manual of your shell about globbing for more information. [[category:UbuntuHelp]]
该页面使用的模板:
模板:From
(
查看源代码
)
模板:Languages
(
查看源代码
)(受保护)
模板:Languages/Lang
(
查看源代码
)(受保护)
返回至
UbuntuHelp:ShellGlobbing
。