[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Filename Expansion: Find Utility Pattern Matching vs Bash Shell Patt

From: Eric Blake
Subject: Re: Filename Expansion: Find Utility Pattern Matching vs Bash Shell Pattern Matching
Date: Tue, 16 Jun 2015 14:34:03 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

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

[Prev in Thread] Current Thread [Next in Thread]