News for hackers. Stuff that matters.
Re:macOS command-line tools you might not know about by paulddraper on Tuesday June 27, 2023 @10:30AM (#36493372)
They avoid an unnecessary invocation of the cat executable.

Instead, they open a file descriptor and pass that.

Tiny difference but there you go.

Re:macOS command-line tools you might not know about by latexr on Tuesday June 27, 2023 @10:37AM (#36493489)
To add, searching for “useless use of cat” will yield several results for those interested in learning more. Other examples include “useless use of echo” and “useless use of ls *”.
Re:macOS command-line tools you might not know about by vram22 on Tuesday June 27, 2023 @07:30PM (#36500160)
Yes.

Even the Wikipedia page on cat has a section about that, titled eponymously.

https://en.m.wikipedia.org/wiki/Cat_(Unix)

Re:macOS command-line tools you might not know about by adrianmonk on Tuesday June 27, 2023 @11:04AM (#36493906)
Not just that, but also all the bytes have to go through an extra pipe. Presumably they're copied an extra time because of this.

When you run "cmd < file", the command reads from stdin, which pulls directly from the file. When you do "cat file | cmd", "cat" opens the file, reads from there, and writes to a pipe. Then "cmd" reads from its stdin, which is a pipe.

Re:macOS command-line tools you might not know about by chlorion on Tuesday June 27, 2023 @08:56PM (#36500885)
GNU cat will use the copy_file_range syscall when possible!

copy_file_range allows a user land program to copy data between two files without doing any user space work. Instead of reading data into a buffer and writing it back out to the destination, the kernel will somehow manage to move the data for you.

I think this will prevent any extra copies from occurring in situations where it can be used.

https://man.archlinux.org/man/copy_file_range.2

https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/cat...

Re:macOS command-line tools you might not know about by omginternets on Tuesday June 27, 2023 @11:53AM (#36494613)
>They avoid an unnecessary invocation of the cat executable.

And ... ?

Re:macOS command-line tools you might not know about by wpm on Tuesday June 27, 2023 @12:07PM (#36494783)
I teach shell scripting. Cat invocations are cheap and help learners understand and keep clear where input is coming from, and where it is going. There are no awards or benefits to reducing the number of lines, commands invoked, or finding the shortest possible way to perform a task in a script. There are plenty of detriments to reading and understanding though when we try to obfuscate this to save 1ms of execution time on a script that is going to execute near instantaneously anyways.

In short, I straight up don't care.

Re:macOS command-line tools you might not know about by revscat on Tuesday June 27, 2023 @12:21PM (#36494997)
I 100% agree with you. My only defense of OP is that `<` is something tends to be forgotten. Like everyone else in this thread I go to `cat` first for things like this. But sometimes I forget that even `<` exists, and the callout is a nice reminder.