Bash
grep 意外行為時0−90−90-9模式在 ubuntu 上給出
當我使用
[0-9]
模式搜尋文件中的所有數字時,是否在模式周圍使用單引號/雙引號會產生不同的輸出。$ cat numbers this line has 3 this line has 4 this line has 2 this line has 8 this line has 1 $ grep [0-9] numbers this line has 1 $ grep '[0-9]' numbers this line has 3 this line has 4 this line has 2 this line has 8 this line has 1
[0-9]
和有什麼區別'[0-9]'
?我用[a-z]
and做了一個類似的測試'[a-z]'
,但結果與前面的例子不同。我在我的 16.04.7 LTS (Xenial Xerus) 機器上對其進行了測試。當我在我的 Mac 上進行相同的測試時,它按預期工作。
我無法在 Arch Linux 上的 Bash 5.1.4 上重現,也無法在 Ubuntu 16.04 上重現:
$ docker run --interactive --rm --tty ubuntu:16.04 /bin/bash # cat > numbers <<EOF > this line has 3 > this line has 4 > this line has 2 > this line has 8 > this line has 1 > EOF # grep [0-9] numbers this line has 3 this line has 4 this line has 2 this line has 8 this line has 1
@Kusalananda 有正確的想法,您可能有一個名為“1”的文件,未引用的 glob 在傳遞給之前會擴展為
grep
:# touch 1 # grep [0-9] numbers this line has 1
答案:使用更多行情™ :)