$_ CLI In Depth
Learning progress 0%
lesson files globbing find

Extended Globs, dotglob & nullglob

6 min read Module 4: File Manipulation, Globbing & find Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

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.

BASH6 lines
cd /workspace
mkdir -p eg
touch eg/a.txt eg/b.txt eg/.secret eg/c.md
shopt -s dotglob
echo eg/*
shopt -u dotglob

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

BASH5 lines
cd /workspace
shopt -s extglob
echo eg/@(a|b).txt
echo eg/!(*.txt)
shopt -u extglob

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

Warning

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.

BASH5 lines
cd /workspace
shopt -s nullglob
echo eg/*.zip
shopt -u nullglob
echo eg/*.zip

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

Globs, brace expansion and dotfiles

Visualizer

The shell expands a pattern against the directory listing before the command runs. Brace expansion happens earlier, and it never looks at the disk.

1 / 5
$ echo {a,b}*.txt
ls of the test directory
a1.txt
a2.log
b.txt
notes.md

Nothing has been expanded yet. The directory listing below is what the glob step will read.

cd "$(mktemp -d /tmp/glob.XXXX)" && touch a1.txt a2.log b.txt notes.md && echo {a,b}*.txt
Terminal lab · 3 steps · real Linux in your browser

Turn on dotglob, extglob and nullglob

Compare the default glob with dotglob, use an alternation with extglob, and show nullglob on an empty match.

01
Include hidden names
In a subshell, turn on dotglob, then save what eg/* expands to: ``bash (shopt -s dotglob; echo eg/* > dotglob.txt) ``
Show hint for step 1
The parentheses keep the option change out of your interactive shell.
02
Match alternatives
Turn on extglob on its own line, then save what eg/@(a|b).txt expands to. The option must be set before the pattern is read, so keep it on a separate line: ``bash shopt -s extglob echo eg/@(a|b).txt > extglob.txt ``
Show hint for step 2
@(a|b) matches exactly one of the two alternatives.
03
Expand an empty match to nothing
In a subshell, turn on nullglob, then save what eg/*.zip expands to. There are no zip files, so the output is one empty line: ``bash (shopt -s nullglob; echo eg/*.zip > nullglob.txt) ``
Show hint for step 3
Without nullglob the output would be the literal text eg/*.zip.
Check yourself Question 1 of 4
Concept Check Hidden names in a glob recall

Which shell option makes wildcards match names that start with a dot?

Recap & Key Takeaways

shopt -s dotglob makes wildcards match hidden names, which is convenient for reads and dangerous for deletes.
extglob adds patterns such as @(a|b) and !(x), and it is a bash feature rather than a POSIX one.
nullglob makes a pattern with no matches expand to nothing, which avoids loops that run once on a literal pattern.
Shell options apply to the whole shell session, so change them in a subshell or restore them afterward.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue