1

I don't think I ever fully grokked the rules of when and why one needs to doublequote things in Linux/Bash (I'm not sure if this problem's domain is Linux, Bash, or something else). I thought that * matched any number of characters (including none), and this seems to align with Wikipedia's article. But when I tried to use the * wildcard as follows, I got results I don't understand:

>pwd
/home/user/foo/foo/test
>tree 
.
├─ main.opp
└─ test
   └─ test.opp

>find ../ -name *.opp
../test/test.opp

>find ../ -name "*.opp"
../main.opp
../test/test.opp

>cd ..
>pwd
/home/user/foo/foo

>find . -name *.opp
./main.opp

>find . -name "*.opp"
./main.opp
./test/test.opp

Why does find with the non-doublequoted *.opp argument only return one hit, whereas doublequoting the same returns the expected two hits?

marked as duplicate by steeldriver, Michael Homer, Jeff Schaller, G-Man, Satō Katsura Jun 6 '17 at 6:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

It depends when the expansion is evaluated. If you don't quote the asterisk, bash evaluates it first. It's equivalent to calling find . -name main.opp. If you quote the asterisk, it's passed as-is to find which uses it the way you expect.

2

Without quote, bash will expand the asterisk if there is any matching files in the current directory. Here I use echo to debug the expansion.

~/foo/foo/test$ echo find ../ -name *.oop
find ../ -name test.oop
~/foo/foo/test$ cd ..
~/foo/foo$ echo find . -name *.oop
find . -name main.oop

If you search for something which does not give a match in the current directory, it will not expand:

~/foo/foo$ echo find . -name test.*
find . -name test.*
~/foo/foo$ find . -name test.*
./test/test.oop

Not the answer you're looking for? Browse other questions tagged or ask your own question.