abs 5.引用 - 通配符 word split Pathname Expansion

[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  characters
       appears,  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 的命令处理流程,展开的先后顺序是:

brace expansion,
tilde expansion,
parameter、variable、arithmetic expansion 、command substitution
word splitting
pathname expansion


其中,pathname expansion 就是所谓的 wildcard。

至于什么是 wildcard, 资料文档很多,懒得说了。

那么 wildcard 发生在哪里?或者说命令行的哪部分进行了 pathname expansion (路径展开)?

bash 文档说,对所有字都进行 pathname expansion, 除非设置了 -f 选项。

这与网中人所说的:

http://bbs.chinaunix.net/viewthr ... ;page=16#pid2930144

首先, wildcard 也是屬於 command line 的處理工序, 作用於 argument 裡的 path 之上.
沒錯, 它不用在 command_name 也不用在 options 上.
了解了 wildcard 的擴展與重組特性後, 接下來, 讓我們了解一些常見的 wildcard 吧:
*: 匹配 0 或多個字元
?: 匹配任意單一字元

    {string1,string2,...}: 匹配 sring1 或 string2 (或更多)其一字串

    
是否有出入?

( 参考 gunguymadman 的贴子  http://bbs.chinaunix.net/thread-1608949-1-1.html  )

很明显,{string1, string2, ...} 不是 wildcard, 而是 brace expansion,不说了。

看实验:
context:
  1. $ echo $BASH_VERSION
  2. 3.2.48(21)-release

  3. $ ls -1
  4. --file=ok.txt*
  5. --files.txt*
  6. -file.txt*
  7. -file=true.txt*
  8. file
  9. file2.txt
复制代码
options:
  1. $ echo -f*
  2. -file.txt -file=true.txt

  3. $ echo --file=*
  4. --file=ok.txt

  5. $ echo --f*
  6. --file=ok.txt --files.txt
复制代码
command:
  1. $ set -vx

  2. $ f* file
  3. f* file
  4. + file file2.txt file
  5. file2.txt: ASCII text
  6. file:      ASCII text

  7. $ set +vx
  8. set +vx
  9. + set +vx
复制代码
所以,应该避免这样的写法:
  1. grep -Eril --inlcude=*.c dir
复制代码
另外,bash 也说,在赋值的 value和 [[ ]]中不执行 pathname expansion,确实如此:
  1. $ a=f*

  2. $ echo "$a"
  3. f*
复制代码

你可能感兴趣的:(abs 5.引用 - 通配符 word split Pathname Expansion)