mapfile chunk limits and the final unterminated line
0 reputation · 18 Dec 2020, 15:46 UTC
I need to define paging semantics for a Bash script that reads a large line-oriented stream with mapfile -n. The page size is a line count rather than a byte limit, and the input may end without a trailing newline.
For Bash 4.0 and later, read can assign a final unterminated line while returning a nonzero status at EOF, whereas mapfile -t removes trailing line delimiters when present. The unresolved design question is how a chunked reader should classify that last line: as a complete record, a partial record, or a separate boundary case. Empty lines and NUL-delimited input introduce additional ambiguity because the delimiter is not represented in the same way.
The target Bash version and its built-in help for mapfile and read should be checked before relying on exact delimiter behavior, particularly if the script must also run under dash or another POSIX shell.
- Should the final unterminated line be included in the last
mapfile -nchunk? - How should an empty final line be distinguished from EOF without a delimiter?
- Is byte-bounded paging required, or is a documented line-count limit sufficient?