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

答え:より多くの引用を使用™:)

おすすめ記事