grep 在 ubuntu 上给出 [0-9] 模式时的意外行为

grep 在 ubuntu 上给出 [0-9] 模式时的意外行为

当我使用[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]和做了类似的测试'[a-z]',但结果与前面的示例相同。

我在我的 16.04.7 LTS (Xenial Xerus) 机器上测试了它。当我在 Mac 上进行相同的测试时,它按预期工作。

答案1

我无法在 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

答案:使用更多报价™:)

相关内容