?, *, and [ ] are interpreted by the globbing engine for filename matching. Preceding these characters with a backslash (\) or quoting them forces the shell to treat them as literal characters.
Using an escape (
\) or quotes disables globbing for the next character or entire string. This gives you precise control over filename creation and listing.1. Creating Sample Files
First, generate files whose names differ only by the first letter:2. Wildcard vs. Literal ?
2.1 Using the ? Wildcard
The ? matches exactly one character. Attempting to create ?ail without escaping:
?ail expanded to all existing matches (fail, hail, etc.).
2.2 Escaping ? for Literal Filenames
To create a file literally named ?ail:
3. Listing Files with a Leading ?
You can retrieve the ?ail file by escaping or quoting the pattern:
4. Mixing Literals and Wildcards
Assume these files exist:?ail:
\?ail* treats \? as literal ? and * as the wildcard for any suffix.
Be cautious: unescaped wildcards can match unintended files. Always check your patterns with
echo or ls before running destructive commands.5. Merging Globs into a Single Pattern
For files namedPail, Pail*, and PailTwo:
* wildcard expands to zero or more characters following Pail.
6. Quick Reference Table
| Special Character | Behavior | Escaped Form | Example |
|---|---|---|---|
? | Single-character match | \? | touch \?file |
* | Zero or more characters | \* | ls Pail\* |
[ ] | Character set | \[ \] | ls \[abc\]* |
Summary
- A backslash (
\) or quotes disables globbing for the next character or entire string. - Use wildcards (
?,*,[ ]) without escaping to match patterns. - Combine escaped literals and wildcards for precise filename operations.