Skip to main content
Bounty Awarded with 500 reputation awarded by tripleee
Mod Moved Comments To Chat
Add a discussion about not-necessarily UUoC commands
Source Link
Jonathan Leffler
  • 760.6k
  • 145
  • 962
  • 1.3k

With the UUoC version, cat has to read the file into memory, then write it out to the pipe, and the command has to read the data from the pipe, so the kernel has to copy the whole file three times whereas in the redirected case, the kernel only has to copy the file once. It is quicker to do something once than to do it three times.

Using:

cat "$@" | command

is a wholly different and not necessarily useless use of cat. It is still useless if the command is a standard filter that accepts zero or more filename arguments and processes them in turn. Consider the tr command: it is a pure filter that ignores or rejects filename arguments. To feed multiple files to it, you have to use cat as shown. (Of course, there's a separate discussion that the design of tr is not very good; there's no real reason it could not have been designed as a standard filter.) This might also be valid if you want the command to treat all the input as a single file rather than as multiple separate files, even if the command would accept multiple separate files: for example, wc is such a command.

It is the cat single-file case that is unconditionally useless.

With the UUoC version, cat has to read the file into memory, then write it out to the pipe, and the command has to read the data from the pipe, so the kernel has to copy the whole file three times whereas in the redirected case, the kernel only has to copy the file once. It is quicker to do something once than to do it three times.

With the UUoC version, cat has to read the file into memory, then write it out to the pipe, and the command has to read the data from the pipe, so the kernel has to copy the whole file three times whereas in the redirected case, the kernel only has to copy the file once. It is quicker to do something once than to do it three times.

Using:

cat "$@" | command

is a wholly different and not necessarily useless use of cat. It is still useless if the command is a standard filter that accepts zero or more filename arguments and processes them in turn. Consider the tr command: it is a pure filter that ignores or rejects filename arguments. To feed multiple files to it, you have to use cat as shown. (Of course, there's a separate discussion that the design of tr is not very good; there's no real reason it could not have been designed as a standard filter.) This might also be valid if you want the command to treat all the input as a single file rather than as multiple separate files, even if the command would accept multiple separate files: for example, wc is such a command.

It is the cat single-file case that is unconditionally useless.

Source Link
Jonathan Leffler
  • 760.6k
  • 145
  • 962
  • 1.3k

With the UUoC version, cat has to read the file into memory, then write it out to the pipe, and the command has to read the data from the pipe, so the kernel has to copy the whole file three times whereas in the redirected case, the kernel only has to copy the file once. It is quicker to do something once than to do it three times.