// learn ยท linux ยท rhcsa ยท 10 min

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.
// globbing and quoting

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.

// before you start
you should know
  • ยท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
by the end you'll

Predict what the shell expands a pattern to before pressing Enter. Choose the right quoting style for each situation.

pace: 10 minutes

bash โ€” step 1 / 6

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.

// key insight

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.

// exam-ready ยท the patterns and switches you must know
$*
$?
$[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 +f
// check yourself
4 quick questions
Q1

Your home directory contains .bashrc, .vimrc, notes.txt, and Documents/. What does `ls *` print?

Q2

How is `{a,b,c}` different from `[abc]`?

Q3

Which one of these prints your username, and which prints the literal string `$USER`?

Q4

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.

๐ŸŽฎ
// now exercise it

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.

play โ†’

Read this lesson as text6 chapters ยท quiz answers

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

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

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

  4. 04Hidden files and globstar (**)

    Filenames starting with a dot are 'hidden' โ€” * deliberately doesn't match them, so rm * doesn't blow away your .bashrc. Match dotfiles explicitly with .* or enable shopt -s dotglob. For recursive matching across subdirectories, enable shopt -s globstar and use **.

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

  6. 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 why rm * doesn't wipe out your dotfiles. To match dotfiles use .* or shopt -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/; done buggy when a filename contains a space?
โ†’Unquoted $f is word-split AFTER variable substitution, so cp sees two arguments instead of one filename.The glob itself produces correct filenames, but expanding $f without quotes lets the shell split on whitespace. Always quote variables: cp "$f" backup/. Same rule applies to "$@" in scripts.
// more in linux

keep going โ€” these pair well with what you just learned.

see all linux โ†’

See also

back to RHCSA trackall lessons