The useful use of cat
Unix users debate whether invoking `cat file | cmd` is a sloppy “useless use of cat” or a perfectly reasonable way to build and read pipelines left-to-right. Comments weigh tiny performance and correctness pitfalls against ergonomics, portability, readability for less-experienced shell users, and the flexibility to later swap `cat` for tools like `pv` or `socat`. The exchange branches into nuances of input redirection syntax, POSIX semantics, shell quirks, and the broader idea that stylistic purity often collides with practicality and teaching value.
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.