Cat without cat on the commandline (2020)

by zoidbon 10/21/2024, 6:41 AMwith 20 comments

by basemion 10/21/2024, 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 silisilion 10/21/2024, 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 jwilkon 10/21/2024, 7:16 AM

Related: https://github.com/wader/catgolf (might not be completely serious)

by ilyagron 10/21/2024, 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 ChrisArchitecton 10/21/2024, 9:02 AM

Related:

The useful use of cat

https://news.ycombinator.com/item?id=41894933

by vman81on 10/21/2024, 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 optymizeron 10/21/2024, 7:14 AM

On what kind of Linux system do you get a shell but don't have access to `cat`?

by its-summertimeon 10/21/2024, 7:42 AM

the read/echo loop will not handle nulls

the command substitution will mangle ending newlines