Glossary

Glob

Also known as: globbing, pathname expansion, wildcard

Globbing (or "pathname expansion") is the process by which the shell expands wildcard patterns into matching filenames before running a command. The classic patterns are * (matches any sequence of characters including none), ? (matches a single character), and [...] (matches any of the enclosed characters). For example, rm *.tmp is expanded by the shell into the list of matching filenames before rm ever runs.

Modern bash and zsh support additional patterns:

shopt -s globstar               # enable ** in bash
ls **/*.py                      # recursive match
echo !(*.txt)                   # extended glob, negation
echo *.{jpg,png,gif}            # brace expansion (not strictly a glob)

Globs are different from regular expressions: * in a glob is like .* in regex. Globs operate on filenames and are handled by the shell; regexes operate on arbitrary text and are usually handled by a tool like grep.

An important gotcha: if a glob matches nothing, bash passes it through literally. ls *.xyz becomes ls *.xyz if no such files exist, and ls then complains "No such file or directory: *.xyz". Setting shopt -s failglob or nullglob changes this behaviour to error or to expand to nothing. Quoting a glob (ls "*.txt") suppresses expansion entirely.

Related terms: Shell, Regular Expression

Discussed in:

Also defined in: Textbook of Linux