Master Linux Shell Wildcards: Why * Can Fail and How to Use Them Correctly
This article explains Linux shell wildcard behavior, demonstrates common pitfalls with examples, and clarifies the differences between wildcards, metacharacters, and escape sequences while outlining the shell's parsing steps for reliable command-line usage.
Linux Shell Wildcards and Metacharacters
Shell wildcards (also called glob patterns) are processed by the shell, not by the individual commands. They appear only in command arguments and are expanded by the shell into matching file names before the command runs. If no match is found, the pattern is passed unchanged as a literal argument.
Example:
ls a.txt b.txt c.old
In this case the pattern *.txt matches a.txt and b.txt, so the command actually executed is ls a.txt b.txt. When a pattern such as d*.txt finds no files, the shell leaves it unchanged, resulting in an error like “cannot access d*.txt: No such file or directory”.
1. Common Shell Wildcards
The most frequently used wildcards are *, ?, [], and {}. They resemble regular‑expression syntax but operate differently and should not be confused.
2. Shell Metacharacters (Special Characters)
Beyond wildcards, the shell defines metacharacters that separate words or control command flow, such as |, &, ;, ( ), < >, and whitespace. These characters affect command parsing rather than filename matching.
3. Shell Escape Characters
To treat a wildcard or metacharacter as a literal, you can use one of three quoting mechanisms: the backslash escape, single quotes, or double quotes. The escape character (\) disables the special meaning of the following character.
Example with escaped wildcard:
ls '\*.txt'
The pattern is no longer expanded, so the command attempts to list a file literally named *.txt and fails if it does not exist.
4. Shell Script Parsing Process
When the shell receives a command line, it performs a series of steps: tokenization, alias expansion, parameter expansion, command substitution, arithmetic expansion, word splitting, pathname expansion (wildcard expansion), and finally execution. Quoting influences which steps are performed. Double quotes skip only the pathname expansion step, while single quotes skip all expansions, passing the text directly to the command.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
