Post History
Overview A "useless use" or "abuse" of cat occurs when a Unix pipeline (sequence of commands that feed into each other, using the shell | or "pipe" operator) includes a call to cat that is unneces...
Answer
#1: Initial revision
## Overview A "useless use" or "abuse" of `cat` occurs when a Unix *pipeline* (sequence of commands that feed into each other, using the shell `|` or "pipe" operator) includes a call to `cat` that is unnecessary for solving the problem. Such pipelines are naturally less efficient (since the OS has to start an additional `cat` process) and require a bit more typing, but these things are unlikely to matter much in normal circumstances. The more interesting impact is social: some newer users may find the version of a pipeline with useless `cat`s easier to understand, while more experienced users may find that it violates their aesthetic sense and betrays a lack of comfort with how the tools are intended to work. Learning how to remove unnecessary `cat` uses from a pipeline can be a helpful exercise for improving one's shell programming skills. ## What is `cat`, and how is it used in pipelines? The `cat` program is used to con`cat`enate one or more sources of input - i.e., read the input sources and output all the content of each, one after the other in sequence. These sources can be either files or the standard input. In particular, `cat` can be given a *single* input which it will simply output on its standard output. (This is different from `echo`, which will output content *directly from its command line*, rather than from sources named there.) Running `cat` by itself will treat its standard input as the single input used; interactively, this results in a prompt where everything you type is simply echoed back to you (a second time, on top of the usual terminal echo) until you use control-D to mark the end of standard input (or force the program to quit with control-C or in some other way). ## Why are the unnecessary uses unnecessary? It's common to see pipelines which use `cat` to open a single file and provide it to the standard input of another program, for example `wc -l`. This is **usually** unnecessary because **the other program already accepts** filename arguments, and could open and read the file itself (and use that input instead of the standard input). In more extreme cases, `cat` by itself does nothing useful in a pipeline. It's like adding an extra length of pipe to a physical pipeline - it does nothing to change the logic of where or when the water flows, or how it splits off or rejoins. For example, `cat | wc -l` is a more wasteful way to write `wc -l`, because the latter *would already* read from standard input and count the lines.