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.
Related: https://github.com/wader/catgolf (might not be completely serious)
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`.
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`
On what kind of Linux system do you get a shell but don't have access to `cat`?
the read/echo loop will not handle nulls
the command substitution will mangle ending newlines
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...