Demi Marie Obenour <demiobenour@gmail.com> writes:
On 9/8/25 04:23, Alyssa Ross wrote:
Demi Marie Obenour <demiobenour@gmail.com> writes:
POSIX requires that the shell read builtin not consume any bytes beyond the end-of-line character. For non-seekable files like pipes, this requirement can only be met by reading one byte at a time, which is very slow. Avoid this by reading the entire input into a temporary file and having sh read from the temporary file. Since regular files are seekable, sh can read many bytes and then seek back to the correct file position.
Slow enough to make a noticeable difference in the context of the whole script?
Don't know 🙂. It's just a known antipattern and I saw bash using a decent amount of CPU time.
Did a quick (imperfect) benchmark of a full make using hyperfine. Actually came out slightly slower with this change, but difference was statistically insignificant. (Without the change came out 1.01 ± 0.08 times faster than with it.) So looks like it just doesn't matter either way.