The useful use of cat
Use cases for cat
- Commonly used to start pipelines so they read left-to-right:
cat file | cmd | .... - Handy for pasting from clipboard into files or pipes, especially when editor formatting is undesirable.
- Used to concatenate multiple files uniformly; using it even for one file keeps patterns consistent.
- Helpful in SSH workflows, e.g. piping to
sudo teeto write protected files, or as part of ad‑hoc transfer tricks. - Frequently combined with tools like
grep,less,head,tail,pv, orsocatin exploratory or debugging workflows.
Redirection vs cat
- Some argue
cat file | cmdis redundant whencmd fileorcmd < fileworks. - Others emphasize that redirection can come first (
<file cmd | ...) and can replacecatwhile preserving left‑to‑right flow. - Several commenters find
<file cmdvisually confusing or obscure, especially at the start of a line, and prefer the explicitness ofcat file | cmd. - Concern is raised about confusion or accidents (
<filevs>file) and shell grammar oddities (e.g., failing with certain loop forms).
Readability, pedagogy, and culture
- One camp sees “useless use of cat” as a sign of not understanding pipes, redirection, or processes.
- Another camp prioritizes readability and familiarity: everyone understands
cat file | grep, fewer understand<file grep. - Some frame the debate as virtue signaling: beginners use
cat, mid‑level users avoid it to appear advanced, more experienced users return tocatfor clarity and to avoid nitpicking. - There is support for “small, understandable subsets” of shell to keep scripts more approachable.
Performance and correctness
- Extra
catprocesses are usually seen as negligible, but there are edge cases wherecatcan be harmful or break optimizations (e.g.,cat | tail,--followbehavior). - A few people note that while attaching a file descriptor directly is more efficient than piping through
cat, the practical difference is small in many real cases.
Alternatives and shell tricks
- Mention of tools like
pv,up,fzf,bat,tac, andteeas complements or replacements in specific scenarios. - Zsh-specific redirection shortcuts (
<fileto page,>fileto capture typed input) and more advanced redirection tricks are discussed, though some find them arcane.