4 ms·
use of cat needs to be discussed too: cat < file | somecmd | cat > file2 #vs somecmd < file > file2
by ggm 4y ago
use of cat needs to be discussed too:
cat < file | somecmd | cat > file2
#vs
somecmd < file > file2
- donio 4y agoThat particular example is an overly silly but I often write cat file | grep ... or cat file | awk '{blah}' for ad-hoc stuff even though I am fully aware that I could easily avoid the cat simply because having the regex or awk code at the very end makes it easier to read, edit or extend with additional pipeline elements.
- billpg 4y agoI prefer the first way, as you can read it left to right and each step is meaningful. "Take the contents of file, process it through somecmd, then save the result to file2." I would hope both commands boil down to exactly the same action. Any overhead from invoking cat (isn't that a shell built-in?) should be negligible.
- ufo 4y agoThe difference with cat is that at least it is looks nicer in some cases. For example, if I want to put the input file at the start of the pipeline, the cat-less version looks weird and confusing. <file1 somecmd >file2 I wish that shells would just optimize the useless cat behind the scenes, so I could use it without soliciting complaints :)