Difficulty: Easy
Correct Answer: ls chap[124]
Explanation:
Introduction / Context:
Shell globbing expands wildcard patterns to matching filenames. Bracket expressions like [124]
select specific characters at a given position, while matches any string (including none). Understanding where and how to use these patterns is critical for selecting the intended files and avoiding accidental matches or omissions.
Given Data / Assumptions:
Concept / Approach:ls chap0[124]
matches filenames beginning with 'chap0' and ending with 1, 2, or 4. It correctly selects chap01, chap02, and chap04. ls chap
lists every name starting with 'chap', which includes the three targets (though not exclusively). In contrast, ls chap[124]
looks for filenames with exactly five characters where the fifth character is 1, 2, or 4 (for example, chap1, chap2). It does not match chap01, chap02, or chap04 because those have six characters with a zero between 'chap' and the final digit. Therefore, that pattern fails to list the required files.
Step-by-Step Solution:
Verification / Alternative check:
Run 'printf "%s\n" chap*' in a test directory and compare expansions for each pattern. Observe that chap[124] never includes the '0' required for chap0[124].
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting that bracket expressions match a single character, misplacing the '0', or assuming globbing uses regex rules like '?' and '+'—shell globs are simpler and differ from regular expressions.
Final Answer:
ls chap[124]
Discussion & Comments