How does globbing work, and what do `nullglob`, `failglob`, `dotglob` and `globstar` change?
IntermediateConceptVariables, quoting and expansions
Answer
Unquoted *, ? and [...] are matched against filenames. If nothing matches, Bash by default leaves the pattern literally, so for f in *.log with no logs runs once with f='*.log'.
shopt -s nullglob # no match -> expands to nothing
shopt -s failglob # no match -> error
shopt -s dotglob # * also matches .hidden files
shopt -s globstar # ** matches recursively: for f in /etc/**/*.conf
shopt -s extglob # !(pattern), +(a|b), @(x|y)
rm -- !(*.keep) # with extglob: remove everything except *.keep
Globs are not regexes: * means "any characters", not "repeat the previous one".
More on Variables, quoting and expansions
What are the rules for assigning variables in Bash?BeginnerWhat is the difference between single quotes, double quotes and `$'...'`?BeginnerWhy should you almost always quote variable expansions? Show what goes wrong.BeginnerExplain the difference between `"$@"`, `"$*"`, `$@` and `$*`.IntermediateWhat do `${var:-default}`, `${var:=default}`, `${var:+alt}` and `${var:?msg}` do?IntermediateHow do you strip prefixes and suffixes with parameter expansion?Intermediate