开发者

implement wildcard expansion in a shell

开发者 https://www.devze.com 2023-02-08 22:58 出处:网络
I am trying to create custom shell as an exercise and wanted to implement wildcard expansion. How exactly do shells like bash perform the expansion? I mean what all steps are involved?

I am trying to create custom shell as an exercise and wanted to implement wildcard expansion. How exactly do shells like bash perform the expansion? I mean what all steps are involved?

As I understand, the shell looks for file names in the current directory and replaces the argument which contains the '*' with the filenames which should match. Is this 开发者_高级运维correct? What other wildcard expansions should a shell do other than a '*'


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.


You can perform wildcard expansion at the time of tokenizing the command given to prompt. Use glob(3) library to perform wildcard expansion. By setting different flags in the glob function, various types of expansion can be performed. glob(3) documentation

Reference for different types of wildcard expansions - Here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号