Skip to content
Mingye Wang edited this page Sep 19, 2015 · 14 revisions

Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

Problematic code:

cat file | tr ' ' _ | grep a_

Correct code:

tr ' ' _ < file | grep a_
< file tr ' ' _ | grep a_ # warn: simple commands only -- won't work with componds

Rationale:

cat is a tool for con"cat"enating files. Reading a single file as input to a program is considered a Useless Use Of Cat (UUOC).

It's more efficient and less roundabout to simply use redirection. This is especially true for programs that can benefit from seekable input, like tail or tar.

Many tools also accept optional filenames, e.g. grep -q foo file instead of cat file | grep -q foo.

Exceptions

None.

Clone this wiki locally