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 withinDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
[[ ... ]] 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.
?(Question Mark): Matches exactly one single character.
[...](Character Class): Matches exactly one character from the enclosed set or range. Ranges are defined using a hyphen (-).
[!...]or[^...](Negated Character Class): Matches exactly one character that is not in the enclosed set.
Extended Globbing (extglob)
Bash provides extended pattern matching capabilities, which must be explicitly enabled in the shell session using the shopt built-in:
pattern-list is a pipe-separated (|) list of one or more patterns:
?(pattern-list): Matches zero or one occurrence of the given patterns.
*(pattern-list): Matches zero or more occurrences of the given patterns.
+(pattern-list): Matches one or more occurrences of the given patterns.
@(pattern-list): Matches exactly one occurrence of the given patterns.
!(pattern-list): Matches anything except the given patterns.
Globbing Modifiers (Shell Options)
The behavior of the globbing engine is modified by several shell options, toggled viashopt -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.
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.Master Bash with Deep Grasping Methodology!Learn More





