Bash globbing and quoting.
Wildcards never reach the command โ the shell expands them first. Watch what gets rewritten, and learn the three quoting styles that stop the shell from rewriting things you didn't mean it to.
Bash globbing and quoting animated tutorial. Shell pattern matching with wildcards, brace expansion, recursive globstar, and the three styles of quoting (single, double, backslash). Covers RHCSA EX200 shell objectives.Two of the most-misunderstood Bash features. Watch each example show what you typed, what the shell expanded it to, and what the command finally saw.
- ยทComfortable running basic commands (ls, cp, mv, rm)
- ยทKnow how to set a variable (FOO=bar) and reference it ($FOO)
- ยทHelpful but not required: read the Command Line Essentials lesson first
Predict what the shell expands a pattern to before pressing Enter. Choose the right quoting style for each situation.
pace: 10 minutes
Globs: the shell rewrites your command before it runs
A glob (also called a wildcard) is a pattern the shell matches against existing filenames. The key idea: the shell expands the pattern into a list of real filenames before the command ever sees it. `*` matches zero or more characters (except a leading dot). `?` matches exactly one character.
The command never sees your * or $VAR. The shell rewrites the line first, then runs the result. Globs are matched against existing filenames; braces are pure text substitution; quoting suppresses one or more of the rewrite steps. When something behaves strangely, ask "what is the shell handing to the command?" โ that's the line you actually ran.
$*$?$[abc]$[a-z]$[!abc] / [^abc]$**${a,b,c}${1..10} / {01..10}$'literal'$"$preserved"$\$$shopt -s dotglob$shopt -s nullglob$shopt -s globstar$shopt -s nocaseglob$set -f / set +fYour home directory contains .bashrc, .vimrc, notes.txt, and Documents/. What does `ls *` print?
How is `{a,b,c}` different from `[abc]`?
Which one of these prints your username, and which prints the literal string `$USER`?
Why is `for f in *.txt; do cp $f backup/; done` buggy when a filename contains a space?
These aren't graded โ they're just for active recall, which is what actually makes the lesson stick.
Try the Bash Command Challenge
Several challenges hinge on getting the glob or the quoting right. Reading is one thing โ typing it yourself and seeing the result is what cements the rules.
Read this lesson as text6 chapters ยท quiz answers
01Globs: the shell rewrites your command before it runs
A glob (also called a wildcard) is a pattern the shell matches against existing filenames. The key idea: the shell expands the pattern into a list of real filenames before the command ever sees it.
*matches zero or more characters (except a leading dot).?matches exactly one character.02Character classes: [abc], ranges, and negation
Square brackets match exactly one character from a set.
[abc]matches a, b, or c.[a-z]matches any lowercase letter โ that's a range.[!abc]or[^abc]matches anything except those characters. Use these when?is too loose and listing every variant is too much typing.03Brace expansion: generative, not pattern-matching
Braces look like globs but work differently.
{a,b,c}and{1..5}are expanded by the shell into a literal list โ regardless of whether any files exist. That makes braces useful for building filenames (mkdir/touch) and command arguments, not just finding things.04Hidden files and globstar (**)
Filenames starting with a dot are 'hidden' โ
*deliberately doesn't match them, sorm *doesn't blow away your.bashrc. Match dotfiles explicitly with.*or enableshopt -s dotglob. For recursive matching across subdirectories, enableshopt -s globstarand use**.05Why quote: spaces, special chars, and variables
Without quotes the shell does three things to your line that you might not want: it splits on whitespace, expands globs, and substitutes variables. Quoting tells the shell 'hands off โ pass this as one literal argument'. The most common Unix bug is forgetting to quote a variable that might contain a space.
06The three quoting styles
Bash has three ways to quote, and they behave differently. Single quotes are absolutely literal โ nothing inside is interpreted. Double quotes preserve spaces but still expand $variables and $(command substitution). The backslash escapes a single character. Pick by what you need to suppress.
Quiz โ answers
- Your home directory contains .bashrc, .vimrc, notes.txt, and Documents/. What does
ls *print? - โDocuments notes.txt
*deliberately skips files that start with a dot. That's whyrm *doesn't wipe out your dotfiles. To match dotfiles use.*orshopt -s dotglob. - How is
{a,b,c}different from[abc]? - โBraces expand to three literal arguments BEFORE looking at the filesystem; brackets match one character against existing filenames.Brace expansion is text substitution โ
mkdir {x,y,z}always creates three directories. Bracket globs are pattern matching โls [xyz]*only lists files that actually exist and start with x, y, or z. - Which one of these prints your username, and which prints the literal string
$USER? - โ
echo "$USER"prints your username;echo '$USER'prints the literal $USER.Single quotes are absolutely literal โ variables don't expand inside. Double quotes preserve spaces but still expand $variables and $(command substitution). - Why is
for f in *.txt; do cp $f backup/; donebuggy when a filename contains a space? - โUnquoted
$fis word-split AFTER variable substitution, socpsees two arguments instead of one filename.The glob itself produces correct filenames, but expanding$fwithout quotes lets the shell split on whitespace. Always quote variables:cp "$f" backup/. Same rule applies to"$@"in scripts.
keep going โ these pair well with what you just learned.
SELinux contexts
Why Apache can't read your file. Watch the policy engine decide โ and learn the workflow for fixing denials. RHCSA-ready.
Linux boot process
From power button to login prompt. Every stage in the chain โ UEFI, GRUB, kernel, initramfs, systemd โ with the rescue moves that save you. RHCSA-ready.
LVM stacking
Layer-by-layer build of an LVM stack: disks โ PVs โ VG โ LVs โ filesystem. Then extend live and snapshot. RHCSA-ready.