Wildcards and simple expansions
On this page
You need to list several notes while leaving a CSV file out. Typing every filename works, but it repeats information already present in the names. A pattern can select the names for you, provided you know which program interprets it.
By the end, you can predict simple filename matches and distinguish that matching from other ways Bash generates words.
Before you start
Section titled “Before you start”Finish PATH and command resolution (B08.4). Use only ~/lab/b08/expansions:
mkdir -p ~/lab/b08/expansionscd ~/lab/b08/expansionstouch note1.txt note2.txt noteA.txt notes.txt report.csv .hidden.txtThese empty disposable files need names, not contents. Keep earlier exercises outside this directory. Use Reset this lesson before repeating the setup.
ls -A.hidden.txt note1.txt note2.txt noteA.txt notes.txt report.csv-A includes .hidden.txt without adding the special . and .. entries, as in B05.3. The capture uses en_US.UTF-8; name ordering can differ under another locale.
Let Bash supply the matching names
Section titled “Let Bash supply the matching names”A wildcard pattern, also called a glob, can expand into existing filenames. * matches zero or more characters within a filename component. Here the suffix .txt restricts the selection:
ls -1 *.txtnote1.txtnote2.txtnoteA.txtnotes.txt-1 is the digit one: it asks ls to print one name per line. Bash expands *.txt before ls runs, supplying four filename arguments. ls receives those names, rather than the pattern.
Notice what is absent: report.csv has a different suffix, and the leading dot of .hidden.txt needs explicit treatment. Nothing was deleted; the command only lists its selected arguments.
Match one character or a selected character
Section titled “Match one character or a selected character”? matches exactly one character. Predict first: does the question mark in note?.txt match only a digit?
ls -1 note?.txtRun it, then compare with your guess
ls -1 note?.txtnote1.txtnote2.txtnoteA.txtnotes.txtNo. It also matches A in noteA.txt and s in notes.txt. The question mark restricts length, not character type.
A bracket pattern selects one character from a set:
ls -1 note[12].txtnote1.txtnote2.txt[12] means one character, either 1 or 2. It does not mean the two-character number twelve. These examples use explicit character sets; range behavior and broader pattern languages need more care than guessing from their appearance.
learner@rootvolt:~/lab$ cd ~/lab/b08/expansionslearner@rootvolt:~/lab/b08/expansions$ ls -1 *.txtnote1.txt
note2.txt
noteA.txt
notes.txtlearner@rootvolt:~/lab/b08/expansions$ ls -1 note?.txtnote1.txt
note2.txt
noteA.txt
notes.txtlearner@rootvolt:~/lab/b08/expansions$ ls -1 note[12].txtnote1.txt
note2.txtlearner@rootvolt:~/lab/b08/expansions$ Files ~/lab/b08/expansions
- ~
- lab/b08/expansions/cwd
- .hidden.txthidden-rw-r--r--
- note1.txt-rw-r--r--
- note2.txt-rw-r--r--
- noteA.txt-rw-r--r--
- notes.txt-rw-r--r--
- report.csv-rw-r--r--
Steps (11)
- Type
cd ~/lab/b08/expansions - Press Enter
- Type
ls -1 *.txt - Press Enter
- Output:
note1.txt note2.txt noteA.txt notes.txt - Type
ls -1 note?.txt - Press Enter
- Output:
note1.txt note2.txt noteA.txt notes.txt - Type
ls -1 note[12].txt - Press Enter
- Output:
note1.txt note2.txt
In words: *.txt and note?.txt each select four names here; note[12].txt selects only note1.txt and note2.txt. The Files panel marks names from the captured output after the command runs. It does not simulate Bash matching a pattern before execution.
| Pattern | Matching fixture names |
|---|---|
*.txt |
note1.txt, note2.txt, noteA.txt, notes.txt |
note?.txt |
note1.txt, note2.txt, noteA.txt, notes.txt |
note[12].txt |
note1.txt, note2.txt |
.*.txt |
.hidden.txt |
*.md |
None; Bash’s tested defaults preserve the unmatched pattern |
A glob is not a regular expression. Those are different pattern rules used by other tools, taught in B10. Do not transfer a regular-expression interpretation to these shell patterns.
Quotes prevent filename expansion
Section titled “Quotes prevent filename expansion”Predict first: will quoting the pattern still supply the four matching names?
printf '[%s]\n' '*.txt'Run it, then compare with your guess
printf '[%s]\n' '*.txt'[*.txt]No. Quoting makes the asterisk literal. printf receives one string containing the pattern characters.
Under this Bash account’s default options, an unmatched pattern is also passed through unchanged:
printf '[%s]\n' *.md[*.md]That result does not prove a file called *.md exists. Bash can be configured to handle unmatched patterns differently; this lesson uses the recorded defaults, without changing shell options.
To select the hidden text file, start the pattern with a dot explicitly:
printf '[%s]\n' .*.txt[.hidden.txt]The match includes the filename, not the file’s contents. None of these commands changes a file.
Generate a path without searching filenames
Section titled “Generate a path without searching filenames”B05 introduced ~ for your home directory. At the beginning of an unquoted word, Bash performs tilde expansion:
printf '[%s]\n' ~[/home/learner]Compare the quoted version:
printf '[%s]\n' '~'[~]The first prints the lab account’s home path; your account can have a different home. The second prints a literal tilde. Neither looks for files matching a pattern. This also explains why ~/lab works in the course’s setup commands but quoting the entire string '~/lab' would prevent that expansion.
Use a command’s printed text
Section titled “Use a command’s printed text”$(...) is command substitution. Bash runs the command inside the parentheses and replaces the substitution with its printed text, removing trailing newline characters:
printf '[%s]\n' "$(printf 'ready\n')"[ready]The inner printf prints ready and a newline. The outer command receives ready as one argument. Double quotes around the substitution preserve its result together, just as they preserve a variable’s expanded value in B08.3.
Use this with commands whose printed result you intend to capture. It is different from typing a command name inside ordinary quotes: the $( and closing ) are what request substitution.
Generate words with braces
Section titled “Generate words with braces”B07’s log setup used {1..39}. The shorter form shows the same brace expansion:
printf '[%s]\n' {1..3}[1][2][3]{1..3} generates the words 1, 2, and 3. A comma-separated list gives alternatives:
printf '[%s]\n' draft{a,b}.txt[drafta.txt][draftb.txt]These words are generated even though neither draft filename exists in the fixture. Brace expansion is not a wildcard and does not require matching files. This is how the numbered setup lines in B07.1 and the B07 practice built their logs.
learner@rootvolt:~/lab/b08/expansions$ printf '[%s]\n' ~[/home/learner]learner@rootvolt:~/lab/b08/expansions$ printf '[%s]\n' '~'[~]learner@rootvolt:~/lab/b08/expansions$ printf '[%s]\n' "$(printf 'ready\n')"[ready]learner@rootvolt:~/lab/b08/expansions$ printf '[%s]\n' {1..3}[1]
[2]
[3]learner@rootvolt:~/lab/b08/expansions$ printf '[%s]\n' draft{a,b}.txt[drafta.txt]
[draftb.txt]learner@rootvolt:~/lab/b08/expansions$ Steps (15)
- Type
printf '[%s]\n' ~ - Press Enter
- Output:
[/home/learner] - Type
printf '[%s]\n' '~' - Press Enter
- Output:
[~] - Type
printf '[%s]\n' "$(printf 'ready\n')" - Press Enter
- Output:
[ready] - Type
printf '[%s]\n' {1..3} - Press Enter
- Output:
[1] [2] [3] - Type
printf '[%s]\n' draft{a,b}.txt - Press Enter
- Output:
[drafta.txt] [draftb.txt]
The transcript shows both home forms, the stable substitution result, three numbers, and two generated draft names. As B08.1 demonstrated, Dash does not perform Bash’s brace expansion for the same input.
Mistakes to recognize
Section titled “Mistakes to recognize”Expecting a glob to search inside files. These patterns select names. They do not inspect the text stored in those files.
Quoting a pattern when you want shell matching. Quotes intentionally protect wildcard characters. Use unquoted patterns only when filename expansion is what you intend.
Passing a destructive command an unchecked pattern. First inspect the match list with ls -1, as above. This lesson only lists or prints selections; it does not remove wildcard-selected files.
Try it yourself
Section titled “Try it yourself”Goal: Select note2.txt and noteA.txt with one bracket pattern, then use command substitution to pass the phrase review complete as one argument to the bracket-printing format.
Hint
The bracket set needs the two alternative characters. Quote the entire substitution so its space remains inside one resulting argument.
Solution
ls -1 note[2A].txtnote2.txtnoteA.txtprintf '[%s]\n' "$(printf '%s' 'review complete')"[review complete]The first result proves the filename selection. The second has one bracket pair around the entire phrase.
Reset this lesson
Section titled “Reset this lesson”Return to the parent and inspect only this lesson’s directory:
cd ~/lab/b08ls -AR expansionsrm -r ~/lab/b08/expansionsRerun the setup under Before you start. If the directory is already absent, skip removal. Close any program using this directory first.
- Unquoted
*,?, and bracket patterns select existing filenames before the command runs. - A leading dot needs explicit matching; quotes protect wildcard characters.
- Tilde expansion supplies a home path; command substitution supplies a command’s printed text.
- Brace expansion generates words without requiring files to exist, and is not wildcard matching.
Next, Practice: How the Shell Interprets Input combines the module’s shell, quoting, variable, lookup, and expansion skills.
Further reading
Section titled “Further reading”- On your system:
man bash, section EXPANSION, andhelp printf. - GNU Bash: Filename Expansion, Pattern Matching, Tilde Expansion, Command Substitution, and Brace Expansion.