[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-bash-read-file-line-by-line-slow-shell":3},{"content":4,"lastModified":5},"\u003Cp>Last Thursday I needed every 404 path out of an 82MB nginx log. My first script read the file line by line in bash and piped each one into \u003Ccode>grep\u003C\u002Fcode>. Two minutes in it had done maybe forty thousand lines, so I killed it and blamed the loop.\u003C\u002Fp>\n\n\u003Cp>It wasn't. Stripped down to a bare count, the loop finished a million lines in 38 seconds. Slow, sure. Not fifty-minutes slow.\u003C\u002Fp>\n\n\u003Cp>Then I reached for \u003Ccode>mapfile\u003C\u002Fcode>, the fix every post recommends, and got \u003Ccode>mapfile: command not found\u003C\u002Fcode>. The bash on this laptop is 3.2.57, and Apple has not shipped a newer major version since 2007.\u003C\u002Fp>\n\n\u003Ch2>One file, six ways to read it\u003C\u002Fh2>\n\n\u003Cp>An 82MB access log, exactly 1,000,000 lines, macOS 26 on Apple silicon, two shells: the stock \u003Ccode>\u002Fbin\u002Fbash\u003C\u002Fcode> at 3.2.57 and bash 5.3.0 compiled from source. Best of two runs each.\u003C\u002Fp>\n\n\u003Ctable>\n  \u003Cthead>\n    \u003Ctr>\u003Cth>Method\u003C\u002Fth>\u003Cth>Time\u003C\u002Fth>\u003Cth>Peak RSS\u003C\u002Fth>\u003C\u002Ftr>\n  \u003C\u002Fthead>\n  \u003Ctbody>\n    \u003Ctr>\u003Ctd>\u003Ccode>wc -l\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>0.08s\u003C\u002Ftd>\u003Ctd>2.7MB\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>awk '\u002F404\u002F'\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>2.20s\u003C\u002Ftd>\u003Ctd>1.8MB\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>while read\u003C\u002Fcode>, bash 5.3\u003C\u002Ftd>\u003Ctd>4.96s\u003C\u002Ftd>\u003Ctd>3.1MB\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>mapfile -t\u003C\u002Fcode>, whole file\u003C\u002Ftd>\u003Ctd>0.27s\u003C\u002Ftd>\u003Ctd>132MB\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>mapfile -t -n 10000\u003C\u002Fcode>, chunks\u003C\u002Ftd>\u003Ctd>0.27s\u003C\u002Ftd>\u003Ctd>4.4MB\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>while read\u003C\u002Fcode>, bash 3.2\u003C\u002Ftd>\u003Ctd>37.99s\u003C\u002Ftd>\u003Ctd>2.8MB\u003C\u002Ftd>\u003C\u002Ftr>\n  \u003C\u002Ftbody>\n\u003C\u002Ftable>\n\n\u003Cfigure>\n  \u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F8554404\u002Fpexels-photo-8554404.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1200\" alt=\"Row of stainless steel ticket gates at a station entrance, each with an Entry arrow and a card reader\" loading=\"lazy\" width=\"720\" height=\"1080\">\n  \u003Cfigcaption>One at a time, in order. Fine for a person, expensive at a million.\u003C\u002Ffigcaption>\n\u003C\u002Ffigure>\n\n\u003Cp>Same script, character for character. 37.99 seconds against 4.96, decided entirely by which binary ran it.\u003C\u002Fp>\n\n\u003Ch2>The shell was the slow part\u003C\u002Fh2>\n\n\u003Cp>The bash 3.2 line shipped in 2007. Apple froze it for licensing reasons and never went back, so 3.2.57 is still what you get when you type \u003Ccode>bash\u003C\u002Fcode> on a clean Mac. \u003Ccode>\u002Fbin\u002Fsh\u003C\u002Fcode> is bash 3.2 too, just in POSIX mode:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\">$ bash --version | head -1\nGNU bash, version 3.2.57(1)-release (arm64-apple-darwin26)\n\n$ sh -c 'echo $BASH_VERSION'\n3.2.57(1)-release\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>\u003Ccode>mapfile\u003C\u002Fcode> only exists from bash 4.0 onward, so the 3.2 row is doing all of that with the older \u003Ccode>read\u003C\u002Fcode>: find the delimiter, copy the bytes, run the body, repeat a million times.\u003C\u002Fp>\n\n\u003Ch2>mapfile is fast, and it costs 132 megabytes\u003C\u002Fh2>\n\n\u003Cp>The speed claim holds. 0.27 seconds against 4.96 is not a rounding error.\u003C\u002Fp>\n\n\u003Cp>The memory claim does not. Reading 82MB into an array peaks bash at 132MB, because every line becomes its own string with its own header and allocation. It gets sold as the efficient choice because it skips the pipe, which it does, then holds the whole file in RAM.\u003C\u002Fp>\n\n\u003Cp>Which is what the chunked row is for: same 0.27 seconds, 4.4MB of peak, because at most ten thousand lines exist at once. The peak follows the chunk size, not the file size.\u003C\u002Fp>\n\n\u003Ch2>The chunked loop that never exits\u003C\u002Fh2>\n\n\u003Cp>This is where I lost an afternoon. The chunked pattern looks correct:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\">while mapfile -t -n 10000 chunk; do\n  process \"${chunk[@]}\"\ndone &lt; access.log\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Run it against a million lines and it spins at 100% CPU. Not slowly. Forever.\u003C\u002Fp>\n\n\u003Cp>Because \u003Ccode>mapfile\u003C\u002Fcode> returns success whether or not it read anything. I watched it hand back a zero-element array and exit 0 over and over, then went to the source: the last statement of \u003Ccode>mapfile()\u003C\u002Fcode> in \u003Ccode>builtins\u002Fmapfile.def\u003C\u002Fcode> is \u003Ccode>return EXECUTION_SUCCESS\u003C\u002Fcode>, and nothing above it returns anything else. No EOF failure path at all. bash 5.3.0 is what I tested, and it loops forever on an empty file too, which is how I believed it.\u003C\u002Fp>\n\n\u003Cp>One more condition fixes it:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\">while mapfile -t -n 10000 chunk &amp;&amp; [ ${#chunk[@]} -gt 0 ]; do\n  process \"${chunk[@]}\"\ndone &lt; access.log\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Without it you have an infinite loop that reads fine in review, and in CI it burns a core until the job times out complaining about something else.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>read\u003C\u002Fcode> does the right thing here: at EOF it returns 1, and that non-zero status is the whole reason \u003Ccode>while read\u003C\u002Fcode> terminates. \u003Ccode>mapfile\u003C\u002Fcode> broke the contract, and the snippet got copied anyway.\u003C\u002Fp>\n\n\u003Ch2>Forking once per line and nothing else matters\u003C\u002Fh2>\n\n\u003Cp>My original script piped every line into \u003Ccode>grep\u003C\u002Fcode>:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\">while IFS= read -r line; do\n  printf '%s\\n' \"$line\" | grep -q ' 404 ' &amp;&amp; hits=$((hits + 1))\ndone &lt; access.log\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Ten thousand lines took 30.55 seconds under bash 5.3, 31.30 under bash 3.2. Two forks per line, 18.8 of those seconds in system time, which is the kernel building and tearing down processes.\u003C\u002Fp>\n\n\u003Cp>Swap the pipe for a builtin match and the same ten thousand take 0.05 seconds:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-bash\">case $line in\n  *' 404 '*) hits=$((hits + 1)) ;;\nesac\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Six hundred times faster, and the shell version stops mattering entirely. At 30.55 seconds per ten thousand lines, my first attempt was a fifty-minute job in a fast script's clothes. I killed it at two minutes on instinct.\u003C\u002Fp>\n\n\u003Cp>If you need a command per line, stop paying per line. Hand the file to \u003Ccode>awk\u003C\u002Fcode>, or split it and run the pieces in parallel as in \u003Ca href=\"\u002Fposts\u002Fxargs-p-parallel-shell-jobs\u002F\">the xargs -P post\u003C\u002Fa>.\u003C\u002Fp>\n\n\u003Ch2>The traps that still bite\u003C\u002Fh2>\n\n\u003Cp>These I re-checked here.\u003C\u002Fp>\n\n\u003Cp>A file that does not end in a newline loses its last line, since \u003Ccode>read\u003C\u002Fcode> returns non-zero on that final chunk. Three lines, the third unterminated: two iterations. Adding \u003Ccode>|| [ -n \"$line\" ]\u003C\u002Fcode> got all three.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>IFS=\u003C\u002Fcode> and \u003Ccode>-r\u003C\u002Fcode> are separate fixes and you want both. \u003Ccode>IFS= read line\u003C\u002Fcode> without \u003Ccode>-r\u003C\u002Fcode> kept two leading spaces intact and still turned \u003Ccode>C:\\logs\\api\u003C\u002Fcode> into \u003Ccode>C:logsapi\u003C\u002Fcode>. One of two looks like it worked, which is worse than neither.\u003C\u002Fp>\n\n\u003Cp>Pipes still run the loop body in a subshell, so a counter set inside \u003Ccode>cat file | while read\u003C\u002Fcode> came out as 0 afterwards. On bash 5, \u003Ccode>shopt -s lastpipe\u003C\u002Fcode> fixes that and the same script reports 2; on bash 3.2 the option does not exist. Under \u003Ccode>set -e\u003C\u002Fcode> a bare \u003Ccode>read\u003C\u002Fcode> outside a loop exits the script on an empty stream, the same class of trap as the \u003Ccode>grep\u003C\u002Fcode> case in \u003Ca href=\"\u002Fposts\u002Fbash-strict-mode-set-euo-pipefail\u002F\">the strict mode post\u003C\u002Fa>.\u003C\u002Fp>\n\n\u003Cp>What is new, at least to me, is how much of that advice depends on a version number nobody prints.\u003C\u002Fp>\n\n\u003Cp>So: \u003Ccode>awk\u003C\u002Fcode> when the job is really text munging, and \u003Ccode>while IFS= read -r\u003C\u002Fcode> when it is shell logic per line, on a bash I have printed the version of. When I do need the lines as an array I chunk them and guard the loop, and that guard lives in \u003Ca href=\"\u002Fsnippetark\u002F\">Snippet Ark\u003C\u002Fa>, because I keep forgetting it and keep rediscovering it at 100% CPU.\u003C\u002Fp>\n","2026-11-26",1795678594070]