[1] | 除非正好当前工作目录下有一个名字为 first的文件. 然而这是引用的另一个原因. (感谢, Harald Koenig, 指出这一点. |
-bash-3.00$ bash --version
GNU bash, version 3.00.16(1)-release (i386-pc-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.
-bash-3.00$ cat test.txt
This is first line
This is First line?
-bash-3.00$ grep '[Ff]irst' *.txt
This is first line
This is First line?
-bash-3.00$ grep [Ff]irst *.txt
This is first line
This is First line?
-bash-3.00$
-bash-3.00$ touch first
-bash-3.00$ grep '[Ff]irst' *.txt
This is first line
This is First line?
-bash-3.00$ grep [Ff]irst *.txt
This is first line
资料总结:
After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these charactersappears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of file names matching the pattern. If no
matching file names are found, and the shell option nullglob is dis-
abled, the word is left unchanged. If the nullglob option is set, and
no matches are found, the word is removed. If the failglob shell
option is set, and no matches are found, an error message is printed
and the command is not executed. If the shell option nocaseglob is
enabled, the match is performed without regard to the case of alpha-
betic characters. When a pattern is used for pathname expansion, the
character ‘‘.’’ at the start of a name or immediately following a
slash must be matched explicitly, unless the shell option dotglob is
set. When matching a pathname, the slash character must always be
matched explicitly. In other cases, the ‘‘.’’ character is not
treated specially.
看blackold的文章:
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3630808&highlight=
按照 bash 的命令处理流程,展开的先后顺序是:
其中,pathname expansion 就是所谓的 wildcard。
至于什么是 wildcard, 资料文档很多,懒得说了。
那么 wildcard 发生在哪里?或者说命令行的哪部分进行了 pathname expansion (路径展开)?
bash 文档说,对所有字都进行 pathname expansion, 除非设置了 -f 选项。
这与网中人所说的:
http://bbs.chinaunix.net/viewthr ... ;page=16#pid2930144
是否有出入?
( 参考 gunguymadman 的贴子 http://bbs.chinaunix.net/thread-1608949-1-1.html )
很明显,{string1, string2, ...} 不是 wildcard, 而是 brace expansion,不说了。
看实验:
context: options: command: 所以,应该避免这样的写法: 另外,bash 也说,在赋值的 value和 [[ ]]中不执行 pathname expansion,确实如此: