- 論壇徽章:
- 5
|
本帖最后由 blackold 于 2011-12-16 10:41 編輯
shell: bash
按照 bash 的命令處理流程,展開的先后順序是:
brace expansion,
tilde expansion,
parameter、variable、arithmetic expansion 、command substitution
word splitting
pathname expansion
其中,pathname expansion 就是所謂的 wildcard。
至于什么是 wildcard, 資料文檔很多,懶得說(shuō)了。
那么 wildcard 發(fā)生在哪里?或者說(shuō)命令行的哪部分進(jìn)行了 pathname expansion (路徑展開)?
bash 文檔說(shuō),對(duì)所有字都進(jìn)行 pathname expansion, 除非設(shè)置了 -f 選項(xiàng)。
這與網(wǎng)中人所說(shuō)的:
http://www.72891.cn/viewthr ... ;page=16#pid2930144
首先, wildcard 也是屬於 command line 的處理工序, 作用於 argument 裡的 path 之上.
沒(méi)錯(cuò), 它不用在 command_name 也不用在 options 上. 了解了 wildcard 的擴(kuò)展與重組特性後, 接下來(lái), 讓我們了解一些常見的 wildcard 吧:
*: 匹配 0 或多個(gè)字元
?: 匹配任意單一字元
{string1,string2,...}: 匹配 sring1 或 string2 (或更多)其一字串
是否有出入?
( 參考 gunguymadman 的貼子 http://www.72891.cn/thread-1608949-1-1.html )
很明顯,{string1, string2, ...} 不是 wildcard, 而是 brace expansion,不說(shuō)了。
看實(shí)驗(yàn):
context:- $ echo $BASH_VERSION
- 3.2.48(21)-release
- $ ls -1
- --file=ok.txt*
- --files.txt*
- -file.txt*
- -file=true.txt*
- file
- file2.txt
復(fù)制代碼 options:
- $ echo -f*
- -file.txt -file=true.txt
- $ echo --file=*
- --file=ok.txt
- $ echo --f*
- --file=ok.txt --files.txt
復(fù)制代碼 command:
- $ set -vx
- $ f* file
- f* file
- + file file2.txt file
- file2.txt: ASCII text
- file: ASCII text
- $ set +vx
- set +vx
- + set +vx
復(fù)制代碼 所以,應(yīng)該避免這樣的寫法:- grep -Eril --inlcude=*.c dir
復(fù)制代碼 另外,bash 也說(shuō),在賦值的 value和 [[ ]]中不執(zhí)行 pathname expansion,確實(shí)如此:請(qǐng)斧正! |
|