Globbing is a programming concept that involves the use of wildcards and special characters to match and filter. Glob patterns are similar to regex patterns, but simpler and limited in scope.

This guide is based on Bash and ZSH.

Resources

Some links copied from begin/globbing

Basics

Source

Wildcard matches are used alongside literals.

Basic wildcards:

  • * - Match any character zero or more times.
  • ** - Match any character zero or more times, including / unlike the others.
  • ? - Match any single character one time.
  • [...] - Match any of the characters. e.g. [abc], [123]
  • [X-Y] - Match a range. e.g. [0-9a-z], [\w]

Example:

  • */* - will match foo/bar.
  • * will match all files and directories in the current directory.
      echo *
    
      ls *
    
      for P in *; do echo $P; done
    

Advanced

Wildcard expansion can be done. This can be previewed with echo but might be used with ls.

echo foo/{bar,baz}
# => foo/bar foo/baz

POSIX character classes

You can also use named ranges, such as:

  • [::alpha::] for any letter.
  • [::alnum::] for any letter or number.
  • [::lower::] for any lowercase character.

e.g.

Match a1 but not aa with:

[[:alpha:][:digit:]]

Match hidden characters

Bash 4

shopt -s dotglob

ZSH

setopt dotglob

Globstar

See the Globstar section.