On 06/16/2015 01:02 PM, Michael Convey wrote:
> For filename expansion, the 'find' utility's '-name' option seems to
> function similarly, but not exactly the same as the bash shell's builtin
> pattern matching.

Correct. bash's builtin pattern matching is implemented as if by using
fnmatch(), and find's -name pattern matching is also implemented as if
by using fnmatch().  However, the two programs likely pass different
flags to fnmatch() (for example, POSIX requires that "find -name '?'"
include ".", while in bash, the glob '?' does not match '.' unless you
have 'shopt -s dotglob' turned on).  There's also the matter that if
configure detects that your libc's fnmatch() is missing or otherwise
broken, then a replacement fnmatch is compiled in, and the replacement
may differ in functionality between bash and find (or even the configure
test deciding if the system fnmatch is broken).

> 
>>From this, it sounds like it is not the shell that is performing the
> pattern matching, but the find utility using the fnmatch library.

If you write:

find -name *.foo

then the shell is using something equivalent to fnmatch to expand *.foo;
and depending on the expansion, you will (usually) end up with a find
syntax error; but (sometimes) end up with a valid command like:

find -name a.foo

If you write:

find -name '*.foo'

then you bypass the shell's use of fnmatch, and find itself sees a
literal pattern to expand using fnmatch.

> 
> Here are my questions:
> 
>    1. Is the bash shell's default filename expansion and pattern matching
>    (extglob shell option disabled) different from that of the find utility
>    using the -name option?

extglob is NOT part of the requirements of fnmatch, so that's a case
where bash is doing things itself (whether that involves breaking the
pattern down into something native fnmatch can handle, or whether the
complete fnmatch replacement comes from bash, is an implementation
detail of bash).  But if extglob is turned on, that's a case of bash
using different flags to fnmatch than find does (as find does not have a
notion of extglob).

>    2. If so, what are those differences?

As mentioned above, there are at least differences with extglob (which
is a bash extension) and dotglob (where matching a leading dot behaves
differently as required by POSIX).

>    3. Does bash also use the fnmatch library or some other mechanism for
>    filename expansion and pattern matching?

That may be a question better asked on the bash list, or by looking in
the bash source code.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to