In a POSIX shell, assume the current directory contains files named 'patch', 'catch', and '.ch' (a hidden file). Using the command cat ch (where globs that do not begin with a dot do not match hidden files), which filenames will be displayed?

Difficulty: Medium

Correct Answer: patch and catch only

Explanation:


Introduction / Context:
Shell globbing rules in Unix/Linux determine which filenames a pattern expands to before a command runs. Understanding how wildcards interact with hidden files (names that begin with a dot) is essential for predictable results, especially when scripting and batch-processing files. This question explores how the pattern ch behaves with visible and hidden files.


Given Data / Assumptions:

  • The directory contains exactly three files: 'patch', 'catch', and '.ch' (the last one is a hidden file since it begins with a dot).
  • The shell is using standard POSIX-like globbing semantics.
  • The command used is cat ch and no shell options (like dotglob or nullglob) have been altered.


Concept / Approach:

By default, shell globs that do not start with a dot do not match hidden files. The pattern ch matches any filename containing the substring 'ch' anywhere in the name, but it will ignore entries that start with '.' unless the pattern itself begins with '.' or shell options are changed. Therefore, 'patch' and 'catch' match, while '.ch' does not.


Step-by-Step Solution:

Identify the pattern: ch (matches any characters, then 'ch', then any characters).Apply the hidden-file rule: patterns not starting with '.' do not match dotfiles.Match visible files: 'patch' (contains 'ch'), 'catch' (contains 'ch').Exclude '.ch': begins with '.', not matched by ch.


Verification / Alternative check:

Run printf '%s\n' ch to preview the shell’s expansion. To include hidden files, you could explicitly write .ch ch or enable dotglob in bash (shopt -s dotglob) and then test again.


Why Other Options Are Wrong:

  • Only .ch: ignores visible matches; incorrect.
  • All three: patch, catch, and .ch: would require a pattern beginning with '.' or dotglob enabled.
  • Only patch: 'catch' also matches; incorrect.
  • None of the above: incorrect because 'patch and catch only' is correct.


Common Pitfalls:

  • Forgetting that hidden files are excluded by default when the pattern does not start with a dot.
  • Assuming regex semantics; globs are simpler and have different rules.


Final Answer:

patch and catch only.

Discussion & Comments

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