implement wildcard expansion in a shell implement wildcard expansion in a shell unix unix

implement wildcard expansion in a shell


The POSIX specification describes the word expansions that POSIX-compliant shells should support. You can use the glob or wordexp POSIX functions to perform these expansions (glob supports only a small subset of the expansions that wordexp supports).


Bourne shell [original sh] supports *, ?, and [range] expansion. bash also supports **


Technically, wildcard expansion is closely related to a pattern matching concept. Very roughly, steps involved include:

  • Translation of a wildcard-containing expression in some sort of runnable form of regular expression or finite state machine.
  • If we're working with FSM, translation of non-deterministic FSM to deterministic one - a process called determinization.
  • Iteration over all possible candidates for matching.
  • Determining whether candidate matches a given wildcard expression by running some sort of matching algorithm using pre-built RE or FSM.
  • Collecting passed candidates together in a list, substitution of wildcard expression with collected list.

As for full range of various characters, take a look at documentation for particular shell implementations (for example, for bash, zsh, etc). Most of these stuff map directly into one or several features of regular expression-like mechanism.