Shell wildcards: Which single wildcard matches exactly one character in UNIX shell globbing?

Difficulty: Easy

Correct Answer: ?

Explanation:


Introduction / Context:
Shell wildcards (globs) expand patterns to filenames and paths. Knowing how ?, , and character classes work is essential for accurate file selection and scripting.



Given Data / Assumptions:

  • We are using typical Bourne-like shells (sh, bash, ksh, zsh) with standard globbing.
  • We need a wildcard that matches one character only.
  • Other glob tokens match sets or arbitrary lengths.


Concept / Approach:
The ? wildcard matches exactly one character. The * wildcard matches any number of characters, including none. Character classes [ijk] match exactly one character but limited to the listed set; [!ijk] matches one character not in the set.



Step-by-Step Solution:

Need exactly one arbitrary character → use ?Need zero or more characters → use Need one character from a set → use [abc]Need one character not in a set → use [!abc]


Verification / Alternative check:
Test with files: touch a1 a2 ab; ls a? matches a1 and a2 but not ab if more characters follow; ls a matches all beginning with a.



Why Other Options Are Wrong:
: Matches zero or more characters, not exactly one. [ijk]: One character but restricted to i, j, or k only. [!ijk]: One character not i, j, or k. None of the above: Incorrect since ? is correct.



Common Pitfalls:
Confusing shell globs with regular expressions; expecting ? to match directory separators when globbing (it does not cross /).



Final Answer:
?

More Questions from Linux

Discussion & Comments

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