Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Globbing is a built-in Bash pattern matching mechanism used for pathname expansion and string evaluation. It evaluates unquoted pattern strings containing specific wildcard characters to match filenames, directories, or text strings. While commonly used to expand paths into alphabetically sorted lists of files before command execution, the exact same globbing engine is utilized for string matching within [[ ... ]] conditional tests, case statements, and parameter expansions (e.g., ${var#pattern}). Glob patterns differ fundamentally from regular expressions in syntax and evaluation rules.

Standard Glob Operators

The standard Bash globbing engine relies on three primary wildcard characters:
  • * (Asterisk): Matches any string of zero or more characters. During pathname expansion, it does not match a leading period (.) representing a hidden file, nor does it match a slash (/) representing a directory separator. However, when used in string matching contexts, the asterisk does match slashes.
echo file*.txt     # Pathname expansion: matches file.txt, file1.txt, file_backup.txt
echo */*           # Pathname expansion: matches files and directories located within immediate subdirectories
[[ "a/b" == a*b ]] # String evaluation: evaluates to true; '*' matches the slash
  • ? (Question Mark): Matches exactly one single character.
echo file?.txt     # Matches: file1.txt, fileA.txt
                   # Does NOT match: file10.txt, file.txt
  • [...] (Character Class): Matches exactly one character from the enclosed set or range. Ranges are defined using a hyphen (-).
echo file[0-9].txt # Matches: file0.txt through file9.txt
echo file[abc].txt # Matches: filea.txt, fileb.txt, filec.txt
  • [!...] or [^...] (Negated Character Class): Matches exactly one character that is not in the enclosed set.
echo file[!0-9].txt # Matches: fileA.txt, file_.txt
                    # Does NOT match: file1.txt

Extended Globbing (extglob)

Bash provides extended pattern matching capabilities, which must be explicitly enabled in the shell session using the shopt built-in:
shopt -s extglob
Extended globs allow for composite patterns, where pattern-list is a pipe-separated (|) list of one or more patterns:
  • ?(pattern-list): Matches zero or one occurrence of the given patterns.
echo file?(1|2).txt   # Matches: file.txt, file1.txt, file2.txt
  • *(pattern-list): Matches zero or more occurrences of the given patterns.
echo file*(A|B).txt   # Matches: file.txt, fileA.txt, fileAB.txt, fileBBA.txt
  • +(pattern-list): Matches one or more occurrences of the given patterns.
echo file+(A|B).txt   # Matches: fileA.txt, fileAB.txt
                      # Does NOT match: file.txt
  • @(pattern-list): Matches exactly one occurrence of the given patterns.
echo file@(1|2).txt   # Matches: file1.txt, file2.txt
                      # Does NOT match: file.txt, file12.txt
  • !(pattern-list): Matches anything except the given patterns.
echo !(file1.txt|file2.txt) # Matches: all files except file1.txt and file2.txt

Globbing Modifiers (Shell Options)

The behavior of the globbing engine is modified by several shell options, toggled via shopt -s (set) and shopt -u (unset):
  • dotglob: Permits the * and ? wildcards to match filenames beginning with a period (.), excluding the special directory references . and ...
  • nullglob: Forces a glob pattern that yields no matches to expand to a null string rather than remaining as an unexpanded literal string.
  • failglob: Causes the shell to throw an expansion error if a glob pattern yields no matches, halting command execution.
  • nocaseglob: Instructs the globbing engine to match alphabetic characters in a case-insensitive manner.
  • globstar: Enables recursive directory traversal. When enabled, the ** pattern matches all files and zero or more directories and subdirectories.
shopt -s globstar
echo **/*.txt      # Matches: all .txt files in the current directory and all nested subdirectories

Escaping and Quoting

To prevent the shell from interpreting glob characters as pattern matching operators, they must be quoted or escaped. When quoted, the characters are treated as string literals.

# Escaping with a backslash
echo \*.txt


# Single quoting (strong quoting)
echo '*.txt'


# Double quoting (weak quoting)
echo "*.txt"
Master Bash with Deep Grasping Methodology!Learn More