Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I thought the wildcard * in the command find . -name *foo* is replaced by bash, and yet the command works unless *foo* matches something in the current directory, then it doesn't. It's confusing.

share|improve this question
3  
Note: If you replace the find . -name *foo* with find . -name '*foo*' it will allow the wildcard matches to work as expected. –  Bill Jul 29 '11 at 22:01

2 Answers 2

up vote 14 down vote accepted

If bash can't find a match, it passes the literal string to the application with *s unexpanded. For example:

$ ls
foo

$ cat /tmp/test
echo $1

$ /tmp/test *foo*
foo

$ /tmp/test *bar*
*bar*

bash expanded *foo* because it matched, but passed *bar* directly because it didn't. The nullglob option will tell bash to resolve non-matching patterns to the empty string instead:

$ shopt -s nullglob
$ /tmp/test *bar*

$
share|improve this answer
1  
A little comment to say that zsh has not the same default behavior, it returns an error: zsh: no matches found. –  Stéphane Gimenez Jul 29 '11 at 19:00
    
zsh has no_nomatch to trigger this behavior. –  chx yesterday

If you want find to get the unexpanded string, then you could quote it or protect the wildcards with backslashes.

$ ls
foo

$ cat /tmp/test
echo $1

$ /tmp/test '*foo*'
*foo*

$ /tmp/test \*foo\*
*foo*
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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