Interview Prep/Shell Scripting/Variables, quoting and expansions

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".

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Variables, quoting and expansions