from Hacker News

Cat without cat on the commandline (2020)

by zoidb on 10/21/24, 6:41 AM with 20 comments

  • by basemi on 10/21/24, 7:24 AM

    The main purpose of `cat` is not to print a single file to stdout but to conCATenate files to stdout [0]. Of course if I have a single file it's printed alone. Just here to say that `cat` is not a synonym of `print`, but nowadays it's "cat FILE" everywhere. Even in APIs... [1] (Compact and aligned text? XD)

    [0] https://www.man7.org/linux/man-pages/man1/cat.1.html [1] https://www.elastic.co/guide/en/elasticsearch/reference/curr...

  • by silisili on 10/21/24, 7:14 AM

    Shell scripting is one of those places where readability is king, and premature optimization is the devil.

    Yes, there are multiple ways to get by without cat. Are they as obvious to understand? If not, what are we doing?

    Shell scripting isn't meant for performance but composability. If you're fighting me over micro optimizations, I'm just going to rewrite it in a proper language and 100x its performance. But then it's lost its readability and it's no longer editable.

    Balance, like most things in life.

  • by jwilk on 10/21/24, 7:16 AM

    Related: https://github.com/wader/catgolf (might not be completely serious)
  • by ilyagr on 10/21/24, 7:27 AM

    Took me a bit to figure out a nice way to do this in the `fish` shell. I think it's `string collect < file.txt`.
  • by ChrisArchitect on 10/21/24, 9:02 AM

  • by vman81 on 10/21/24, 7:54 AM

    If I didn't have manpages or the option to google I'd probably just use head or tail after doing 'wc -l' to determine line count.

    head filename -n `wc -l filename`

  • by optymizer on 10/21/24, 7:14 AM

    On what kind of Linux system do you get a shell but don't have access to `cat`?
  • by its-summertime on 10/21/24, 7:42 AM

    the read/echo loop will not handle nulls

    the command substitution will mangle ending newlines