Shell wildcards (globbing): Which wildcard pattern matches exactly one character in a filename (for example, file?.txt matches file1.txt but not file10.txt)?

Difficulty: Easy

Correct Answer: ?

Explanation:


Introduction / Context:
Shell globbing provides powerful filename expansion. Understanding the differences between , ?, and bracket expressions enables precise selection of files, making scripts and one-liners safer and more predictable.


Given Data / Assumptions:

  • You are using a standard POSIX-like shell (bash, dash, ksh).
  • You want to match exactly one character at a specific position.
  • You do not wish to use regular expressions—this is shell globbing.


Concept / Approach:
The wildcard “?” matches exactly one character. The asterisk “” matches zero or more characters. Bracket classes like “[ijk]” match exactly one character but only from the listed set i, j, or k. A negated class like “[!ijk]” matches exactly one character that is not in the set. Therefore, the general-purpose “single character wildcard” is “?”.


Step-by-Step Solution:

Identify the requirement: any single character, unrestricted.Choose the “?” wildcard, which fits this requirement.Use examples: “file?.txt” matches “file1.txt” and “fileA.txt”, but not “file10.txt”.


Verification / Alternative check:
Echo patterns to see expansions: “echo file?.txt” and compare to “echo file.txt” to visualize differences in matching.


Why Other Options Are Wrong:
: matches any number of characters (including none), not exactly one. [ijk]: matches exactly one, but only i, j, or k. [!ijk]: matches exactly one character that is not i, j, or k. None: incorrect because “?” is correct.


Common Pitfalls:
Confusing globbing with regex; forgetting that hidden files (dotfiles) are not matched by “” or “?” unless dotglob is set or the pattern begins with a dot.


Final Answer:
?

More Questions from Unix

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion