Which shell glob correctly concatenates all files whose names start with 'emp' and are followed by a single non-numeric character?

Difficulty: Medium

Correct Answer: cat emp[!0-9]

Explanation:


Introduction / Context:
Filename expansion (globbing) in POSIX shells lets you select groups of files matching patterns without invoking full regular expressions. Understanding bracket expressions, ranges, and negation is essential for writing concise commands to process batches of files safely and correctly.


Given Data / Assumptions:

  • We are in a POSIX shell (bash/sh/ksh/zsh) with standard globbing.
  • We want files named like empX where X is any non-digit character.
  • We intend to concatenate these files' contents.


Concept / Approach:

Shell globs support bracket lists and negation. Inside [], a leading ! negates the class. [!0-9] therefore matches any single character that is not 0 through 9. Prepending emp makes emp[!0-9], which matches names starting with emp followed by exactly one non-digit. To concatenate contents, use cat with that glob. If the requirement were “one or more” non-digits, you would add a trailing , but the question states a single character.


Step-by-Step Solution:

Select negated digit class: [!0-9].Prefix with literal 'emp': emp[!0-9].Use cat to concatenate: cat emp[!0-9].Execute to read all matching files in lexical order.


Verification / Alternative check:

Use printf '%s\n' emp[!0-9] to preview matches safely before concatenating. This confirms the glob resolves to the intended set of filenames.


Why Other Options Are Wrong:

  • more [emp][!0-9]: matches names two characters long from character sets, not 'emp' prefix.
  • cat emp[x-z]: restricts to letters x–z only, not all non-digits.
  • cat emp[a-z]: matches only lowercase letters; digits excluded but overly restrictive vs “non-numeric.”
  • None of the above: incorrect because cat emp[!0-9] is valid.


Common Pitfalls:

  • Confusing shell globs with regular expressions; their syntax differs.
  • Forgetting that [!0-9] matches exactly one character; append for multiple.


Final Answer:

cat emp[!0-9].

Discussion & Comments

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