Skip to content

Wildcards and simple expansions

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

Finish PATH and command resolution (B08.4). Use only ~/lab/b08/expansions:

Terminal window
mkdir -p ~/lab/b08/expansions
cd ~/lab/b08/expansions
touch note1.txt note2.txt noteA.txt notes.txt report.csv .hidden.txt

These empty disposable files need names, not contents. Keep earlier exercises outside this directory. Use Reset this lesson before repeating the setup.

Terminal window
ls -A
Captured output
.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.

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:

Terminal window
ls -1 *.txt
Captured output
note1.txt
note2.txt
noteA.txt
notes.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?

Terminal window
ls -1 note?.txt
Run it, then compare with your guess
Terminal window
ls -1 note?.txt
Captured output
note1.txt
note2.txt
noteA.txt
notes.txt

No. 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:

Terminal window
ls -1 note[12].txt
Captured output
note1.txt
note2.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.

Match filenames
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.txtlisted-rw-r--r--
      • note2.txtlisted-rw-r--r--
      • noteA.txt-rw-r--r--
      • notes.txt-rw-r--r--
      • report.csv-rw-r--r--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. After each ls command, listed marks identify the fixture names printed by that command.
Steps (11)
  1. Type cd ~/lab/b08/expansions
  2. Press Enter
  3. Type ls -1 *.txt
  4. Press Enter
  5. Output: note1.txt note2.txt noteA.txt notes.txt
  6. Type ls -1 note?.txt
  7. Press Enter
  8. Output: note1.txt note2.txt noteA.txt notes.txt
  9. Type ls -1 note[12].txt
  10. Press Enter
  11. 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.

Predict first: will quoting the pattern still supply the four matching names?

Terminal window
printf '[%s]\n' '*.txt'
Run it, then compare with your guess
Terminal window
printf '[%s]\n' '*.txt'
Captured output
[*.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:

Terminal window
printf '[%s]\n' *.md
Captured output
[*.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:

Terminal window
printf '[%s]\n' .*.txt
Captured output
[.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:

Terminal window
printf '[%s]\n' ~
Captured output
[/home/learner]

Compare the quoted version:

Terminal window
printf '[%s]\n' '~'
Captured output
[~]

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.

$(...) is command substitution. Bash runs the command inside the parentheses and replaces the substitution with its printed text, removing trailing newline characters:

Terminal window
printf '[%s]\n' "$(printf 'ready\n')"
Captured output
[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.

B07’s log setup used {1..39}. The shorter form shows the same brace expansion:

Terminal window
printf '[%s]\n' {1..3}
Captured output
[1]
[2]
[3]

{1..3} generates the words 1, 2, and 3. A comma-separated list gives alternatives:

Terminal window
printf '[%s]\n' draft{a,b}.txt
Captured output
[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.

Generate words without matching files
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Tilde, command substitution, and braces generate words by different rules; they do not all search for matching files.
Steps (15)
  1. Type printf '[%s]\n' ~
  2. Press Enter
  3. Output: [/home/learner]
  4. Type printf '[%s]\n' '~'
  5. Press Enter
  6. Output: [~]
  7. Type printf '[%s]\n' "$(printf 'ready\n')"
  8. Press Enter
  9. Output: [ready]
  10. Type printf '[%s]\n' {1..3}
  11. Press Enter
  12. Output: [1] [2] [3]
  13. Type printf '[%s]\n' draft{a,b}.txt
  14. Press Enter
  15. 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.

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.

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
Terminal window
ls -1 note[2A].txt
Captured output
note2.txt
noteA.txt
Terminal window
printf '[%s]\n' "$(printf '%s' 'review complete')"
Captured output
[review complete]

The first result proves the filename selection. The second has one bracket pair around the entire phrase.

Return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b08
ls -AR expansions
Terminal window
rm -r ~/lab/b08/expansions

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