Edit: Question ansewered by Gordon Davisson's comment

I was reading the GNU Bash manual, and I noticed that there are basically three types of "Parameter expansion" that do pattern matching:

  1. ${parameter#word} ${parameter##word}
  2. ${parameter%word} ${parameter%%word}
  3. ${parameter/pattern/string} ${parameter//pattern/string} ${parameter/#pattern/string} ${parameter/%pattern/string}

What I found odd is that the first two kinds are described like this:

The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching).

But the third kind is described like this:

The pattern is expanded to produce a pattern just as in filename expansion. [...] The match is performed according to the rules described below (see Pattern Matching).

So I'm wondering, is there actually a difference between the expansions? The first two descriptions are not exactly clear about what kind of expansion is made, but, in any case, the "pattern matching" rules are virtually the same as "filename expansion".

So maybe this is an issue of lack of clarity in the documentations, and we can say that they are all the same?

My guess would be that the first two descriptions are more accurate, because I doubt that any sort of actual filesystem check is performed during the third kind.

  • 1
    bash uses the same pattern matching rules (with a few subtle differences) for all three types of expansion, and for filename expansion (and case statements, and...). The description of the third points out the similarity, but it's there in all three. BTW, the type of pattern used here is called "wildcard" or "glob" patterns (and it should not be confused with regular expressions). Jul 2 at 3:04

1 Answer 1


Pattern matching refers to the idea that the pattern a*k matches aardvark.

Filename expansion uses pattern matching. It scans directory entries in the file system, collecting names which match a given pattern.

In the shell, pattern matching is used not only for file name expansion. For instance, it's in the case statement:

case $file in
  *.jpg ) do something with jpg file 
  *.gif ) do something with gif file
  * ) fallback case

POSIX defines two C library functions called glob and fnmatch, which do filename expansion and pattern matching, respectively. There are some differences between them. (Shell implementations don't necessarily use those functions, but implement their own pattern matching and file expansion.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .