Globbing options and hidden files
How it works
Bash options change globbing behavior. Use shopt -s option to enable and shopt -u option to disable. nullglob removes unmatched patterns; failglob reports an expansion error and does not execute the affected command. If both are enabled, failglob takes priority. dotglob includes normally hidden names; globstar enables recursive ** path matching. nocaseglob ignores case when matching names. extglob adds patterns such as @(a|b) and must be enabled before Bash parses constructs using that syntax. A nonempty GLOBIGNORE filters matching names and enables dotglob as a side effect; unset it to disable that side effect (unsetting also turns off dotglob). Do not assume hidden files remain excluded. globskipdots, enabled by default in Bash 5.2, prevents matching . and .. through globbing. These options are Bash-specific; run examples separately in fresh empty directories.
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.
Remove unmatched patterns
Expected stdout:
0Unlike the default, there is no literal fallback argument.
Include hidden names
touch visible.txt .hidden.txtprintf '%s\n' *.txtshopt -s dotglobprintf '%s\n' *.txtExpected stdout:
visible.txt.hidden.txtvisible.txtThe second expansion includes the dot-prefixed file.
Recursive matches
mkdir sub && touch root.txt sub/child.txt && shopt -s globstar && printf '%s\n' **/*.txtExpected stdout:
root.txtsub/child.txtWith globstar, **/ can cover zero or more directory levels. The && chain runs the final print only if fixture setup and option activation succeed.
Common mistakes
With nullglob an unmatched pattern supplies zero arguments, but printf with no data arguments may still print an empty field. Inspect $# instead. Do not enable recursive globs for uncontrolled destructive operations. Options persist in the current shell, so use separate scripts.
Practice
Create visible.txt and .hidden.txt in a fresh directory. Compare argument counts for *.txt before and after enabling dotglob. Also predict the count for an unmatched pattern with nullglob enabled.
Next: Process substitution