Streaming, batching and memory
Streaming processes records incrementally instead of collecting the entire input in memory. Batching groups a bounded number of records to reduce repeated calls. These are different techniques and can be combined. Bash arrays and command substitution hold data in the shell; command substitution also removes trailing newlines and cannot preserve NUL bytes. A while IFS= read -r loop reads line records; include a final unterminated record when the input contract permits it. Use NUL-delimited records for filenames as taught earlier.
Choose bounded state
mapfile -n N -t array reads up to N lines, and repeated calls on an open descriptor can consume successive batches. Test array length, because reaching EOF need not make mapfile return nonzero. A streaming loop still needs memory for a record, and a single giant record can be expensive. Some tools such as sort may buffer or use temporary storage; a pipeline does not automatically ensure constant memory. Bounded workers limit concurrency, not individual task memory. Preserve ordering if required and check component statuses with pipefail. An intentional early consumer such as head can close a pipe before its producer finishes; investigate nonzero statuses rather than suppressing them globally. Prefer deterministic data and explicit limits. Check fixture writes, descriptor opens and output writes. A nonzero pipeline result must stop subsequent reporting; these examples assume small, stable local text fixtures, not arbitrary streaming I/O.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq, GNU coreutils, GNU tar and flock are external dependencies for selected examples. Use only synthetic files in a private directory.
Stream lines without losing the last record
printf 'alpha\n\nomega' > input.txt || exit 1count=0while IFS= read -r line || [[ -n $line ]]; do printf '[%s]\n' "$line" || exit 1 count=$((count+1))done < input.txt || exit 1printf 'count=%s\n' "$count"Expected stdout:
[alpha][][omega]count=3Empty lines are records and the final nonempty unterminated line is retained. Redirect the loop instead of piping into it to preserve the counter.
Consume bounded batches
printf '%s\n' a b c d e > input.txt || exit 1exec 3< input.txt || exit 1while :; do mapfile -u 3 -n 2 -t batch || exit 1 ((${#batch[@]})) || break printf 'batch=%s\n' "${#batch[@]}" || exit 1 printf '%s\n' "${batch[@]}" || exit 1doneexec 3<&- || exit 1Expected stdout:
batch=2abbatch=2cdbatch=1eAt most two records are retained in batch, although record lengths are not bounded here. The last batch may be shorter.
Tee a stream to an audit file
set -o pipefailprintf '%s\n' alpha beta gamma | tee records.txt | wc -l || exit 1head -n 1 records.txt || exit 1tail -n 1 records.txtExpected stdout:
3alphagammatee copies the stream to a file and stdout; head and tail inspect the resulting file rather than prematurely stopping the producer.
Common mistakes
Do not use for line in $(cat file) for arbitrary lines. Avoid a mapfile without a limit when the input size is unbounded, and avoid treating EOF as a fatal error in a normal streaming loop.
Practice
Process a file of seven records, including a blank record and a final line without LF. Compare a streaming count and batches of three. Then test an empty file. State the maximum number of retained records, not an invented memory measurement.
Next: Production failure recovery patterns