Why would this read loop be so much slower in Bash?

November 5, 2026 3 answers
Asked by pocketmeteor

A loop using the same find command reportedly takes about a second with dash and around 15 seconds with Bash. What could explain a gap that large, and how should I test it?

Answers

KRAKLE

If the loop reads a lot of lines, the shell may be doing a huge amount of work one line at a time. Bash and dash have different implementations and features, so their per-line overhead can differ. That could make dash faster, but a 15-fold gap is hard to explain confidently without seeing the exact loop.

If find takes about the same time in both tests, focus on what happens after each line is produced. Share a small version of the script and its input size, and compare the shell loop separately from the find command.

pocketmeteor:

The find command seems to take about the same time in both shells. The slowdown appears to be in the part that reads its output.

Reply to this answer
n0t_a_map

Iโ€™d be cautious about the timing before blaming Bash. Run both versions repeatedly on the same input, alternate which one runs first, and compare real, user, and system time. Also make sure each script really is running under the intended shell and that the tests do identical work.

Without the script, 1 second versus 15 seconds is a clue, not a diagnosis. A tiny difference in how the loop is structured can matter a lot when it runs once per line.

Reply to this answer
sidequesting

For a big stream of text, try doing the processing in awk instead of sending every line through a shell loop. Itโ€™s often a much better fit for line-by-line filtering or matching, and avoids making the shell handle each record itself.

If you need shell logic for each item, keep the loop and benchmark it with a fixed input. The result can depend on the work inside the loop, not just which shell is reading.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions