Shell options change expansion
Bash controls several glob behaviors with options that you set with shopt. A setting applies to the current shell, and it is seen by every command you run after it. Changing an option in an interactive session affects everything that follows, so it is good practice to set options inside a subshell or a script, and to restore them when you finish. The command shopt with no arguments lists the options and their state.
cd /workspace
mkdir -p eg
touch eg/a.txt eg/b.txt eg/.secret eg/c.md
shopt -s dotglob
echo eg/*
shopt -u dotglobWith dotglob on, the asterisk matches the hidden file .secret as well. Bash does not match the special entries . and .. in a glob by default, so the output lists only real names. The final command turns the option off again.
dotglob: including hidden names on purpose
The dotglob option makes the wildcard match names that start with a dot. It is useful for operations on everything in a directory, such as copying the contents of a configuration directory including its dotfiles. Be careful with commands that delete. A pattern such as rm -r * with dotglob on removes hidden files too, which is often more than you intended. The safer habit is to name the hidden files explicitly, such as .[!.]*, which matches dot names except the two special ones.
extglob: richer patterns
With the extglob option on, bash understands a set of patterns written with parentheses. The form @(a|b) matches exactly one of the alternatives, ?(x) matches zero or one occurrence, *(x) matches zero or more, +(x) matches one or more, and !(x) matches anything except x. These patterns let you write a filter in one word, which is convenient for cleaning up a directory.
cd /workspace
shopt -s extglob
echo eg/@(a|b).txt
echo eg/!(*.txt)
shopt -u extglobThe first command matches a.txt and b.txt. The second matches every name in eg that does not end in .txt, which here is the Markdown file and the hidden file is excluded by the usual leading dot rule.
Extended patterns are a bash feature, not a POSIX one. A script that turns on extglob depends on bash, and a script run under another shell may treat the same text differently. Put a bash shebang on any script that uses them.
nullglob: an empty result is an empty list
By default, a pattern that matches nothing is passed through as the literal text. A loop over *.zip in a directory without zip files runs once, with the word *.zip, and the command then fails on a file that does not exist. The option nullglob changes this behavior so that a pattern with no matches expands to nothing. The loop body does not run, and the command has no arguments.
cd /workspace
shopt -s nullglob
echo eg/*.zip
shopt -u nullglob
echo eg/*.zipWith nullglob on, the first echo prints an empty line. With it off, the second echo prints the literal pattern.
failglob and globstar
The option failglob makes a pattern with no matches an error, which stops the command from running. It is a strict alternative to nullglob. The option globstar adds the double asterisk, which matches directories recursively, so **/*.c finds C files at any depth. Both options need care: globstar can descend into large trees and can follow directories you did not intend, and in a large tree find is usually the better tool.
Guidance for scripts
Set options as close to the place they matter as you can, and restore the previous state afterward. Prefer the default behavior when it is enough. When you need nullglob, write the option at the top of the loop, and test the loop on an empty directory as well as a full one, because the empty case is where most surprises are hiding.
Choosing the right option for the job
Most scripts need none of these options. The default glob, which skips hidden names and leaves unmatched patterns alone, is the safest choice for reading files. Add dotglob only when a loop must include hidden files, and add nullglob when a loop must do nothing for an empty match. Use extglob when a pattern with alternatives is clearer than two commands, and only in scripts that declare bash.
A useful test for any glob in a script is to run it in an empty directory and in a directory that contains a file with an unusual name. The empty case shows whether the loop body runs by accident, and the unusual name shows whether quoting is correct. Test both before you trust the script.
Scoping shell options
Options are shell state, so changing them in a script affects the rest of that script. Put the option change inside a function or a subshell when you need it for one block. The pattern (shopt -s nullglob; for f in *.zip; do echo "$f"; done) leaves the outer shell unchanged. This habit makes scripts easier to combine, because a function does not silently alter the behavior of the caller.
Reading a glob before you run it
A practical habit is to echo a glob before you use it with a destructive command. The echo shows exactly what the shell will pass, and it costs nothing. Shell options change that list, so echo the pattern again after changing an option. A pattern that looked safe with the default settings may match hidden files or fail silently once dotglob or nullglob is set, and the echo is the fastest way to notice.
Combine the habit with a count. The command echo pattern | wc -w tells you how many words the shell produced, and a number that is larger than expected is the earliest warning that a pattern is too broad.
Reading a pattern before you run it
A pattern in a script is easier to review when the expansion is visible. Put an echo in front of the command in a dry run, so the shell prints the words it would pass. With extended and hidden-file options, the printed list can differ from what you expect, and seeing it is the cheapest check you can make. A command that deletes should always get that dry run first.
Keep option changes close to the loop or command that needs them. A pattern that depends on three shell options is hard to read, and a reader may not notice that dotglob is on. Write a comment that names the option when you change it, so the next person knows the state of the shell.
Practice in the lab
The lab shows dotglob, extglob, and nullglob, and it saves the output of each in /workspace. The checks look at the words each option produced, so the results must match the behavior of the option you were asked to use.