findコマンドで「一重引用符」を使用することと「一重引用符」を使用しないことの違いは何ですか?

findコマンドで「一重引用符」を使用することと「一重引用符」を使用しないことの違いは何ですか?
find ~/ -name *test.txt
find ~/ -name '*test.txt'

最初のフォームは失敗しますが、2番目のフォームはまだ機能する例を作成する必要があります。

ベストアンサー1

引用符はシェルワイルドカード拡張からコンテンツを保護します。そのコマンドを実行すると(または単純にファイルがあるディレクトリで実行echo *test.txtfootest.txt、最後にファイルがないディレクトリで実行した場合test.txt)、違いを確認できます。

$ ls
a  b  c  d  e
$ echo *test.txt
*test.txt
$ touch footest.txt
$ echo *test.txt
footest.txt

検索でも同じことが起こります。

$ set -x
$ find . -name *test.txt
+ find . -name footest.txt
./footest.txt
$ find . -name '*test.txt'
+ find . -name '*test.txt'
./footest.txt
$ touch bartest.txt
+ touch bartest.txt
$ find . -name *test.txt
+ find . -name bartest.txt footest.txt
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
$ find . -name '*test.txt'
+ find . -name '*test.txt'
./bartest.txt
./footest.txt

おすすめ記事