Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

use of cat needs to be discussed too:

  cat < file | somecmd | cat > file2
  #vs  
  somecmd < file > file2


That 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.


FWIW redirection is not positional:

    ~$ <frongle.txt grep fringle >frungle.txt


We all do. Well probably not Rob Pike


The 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 :)


it's a short walk from shells that optimize "known commands" to signed apps and an ecosystem you can't contribute to unless you've paid the appropriate gatekeeper


Shells already implement many commands as shell builtins. For example, echo, true, and so on.


I 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.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: