Globbing and filename expansion
How it works
Filename expansion (globbing) replaces unquoted patterns with matching pathnames. * matches zero or more characters within a path component, ? matches one character, and [ab] matches one character from the set. Slash must be matched explicitly; leading dots normally require an explicit dot. Quoting a pattern prevents globbing. By default Bash leaves an unmatched pattern as literal text. Matches are sorted according to the locale; the examples use C locale. Globs are not regular expressions. Run each file example in a fresh empty practice directory; touch creates empty files here.
Run each example separately in a noninteractive Bash 5.2 script in a fresh empty practice directory, with default options and C locale. Output is shown separately; do not enter it as code.
Match by extension
touch 'daily report.txt' notes.txt image.pngprintf '[%s]\n' *.txtExpected stdout:
[daily report.txt][notes.txt]The filename with a space remains one argument.
Single-character patterns
touch a1.log a2.log a12.log b1.logprintf '%s\n' a?.logprintf '%s\n' '[ab]1.log' [ab]1.logExpected stdout:
a1.loga2.log[ab]1.loga1.logb1.logThe question mark matches one character; the quoted bracket pattern stays literal.
No match by default
Expected stdout:
1[*.missing]The unmatched pattern remains one literal argument.
Common mistakes
Never parse ls output to reconstruct names. A pathname generated by globbing is already a single argument, even when it contains spaces. When there are no matches, the default literal pattern is not proof that a file exists.
Practice
In a fresh empty practice directory, create log-a.txt, log-b.txt, and log-ab.txt; use a glob to print only the first two names.
Next: Globbing options and hidden files