花括号展开
波浪号展开
HOME
shell variable. If
HOME
is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.)
Shell参数和变量展开
命令替换
算术展开
进程替换
字词分隔
The shell treats each character of $IFS
as a delimiter, and splits the results of the other expansions into words on these characters. If IFS
is unset, or its value is exactly "'<space><tab><newline>'", the default, then any sequence of IFS
characters serves to delimit words. If IFS
has a value other than the default, then sequences of the whitespace characters "space" and "Tab" are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS
(an IFS
whitespace character). Any character in IFS
that is not IFS
whitespace, along with any adjacent IF
whitespace characters, delimits a field. A sequence of IFS
whitespace characters is also treated as a delimiter. If the value of IFS
is null, no word splitting occurs.
Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.)
文件名展开
进行字词分隔后,如果不指定-f选项,shell会搜索"*","?","[",如果遇到了,就会认为是一个带pattern的word,然后用字典序将符合的所有文件名替换过去,如果没有文件名匹配:1 shell的nullglob选项关闭,则不进行文件名展开,保留word原样;2 shell的nullglob打开,则移除这个word。如果shell的nocaseglob选项打开,则忽略大小写。
当匹配文件名时(这里指不包括文件夹),除非shell的dotglob被设置,否则.或./开头的文件都必须显示指定,例如:
ls * # 列出当前文件夹中所有不以"."开头的文件
ls .* # 列出当前文件夹中所有以"."开头的文件
当匹配文件名时,"/"必需显示匹配,例如:
ls ./* 和 ls ./*/*是不同的。
其它情况下,"."和普通字符一样,例如:
*.txt和*txt都可以匹配a.txt
还有一个系统变量GLOBIGNORE,如果一个文件名匹配了一个pattern word,但是它也匹配了GLOBIGNORE,则它会被忽略,不过两个特殊文件一定会被忽略,就是"."和".."。
如果GLOBIGNORE打开,那么dotglob选项也会自动打开,这样会导致当你ls *时,其他以"."开头的文件也会被match,如果想忽略"."开头的文件,可以在GLOBIGNORE里面添加一个".*"的匹配。如果GLOBIGNORE未设定,则dotglob关闭。
参考:
[1] http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html