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?

StoneThrow
  • 1,717

2 Answers2

3

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.

ppbitb
  • 168
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
hschou
  • 2,910
  • 13
  • 15