What this thread does
Do first: feed the loop through done < <(producer) so the while command stays in the current Bash shell.
Short answer
As of November 2026, use the Bash 5.3 manual and maintainer NEWS. A pipeline normally puts its loop in a subshell; assignments there cannot change the parent. This remains current behavior, not a historical defect.
Choose / don't choose
- Process substitution: choose when interactive job control must stay enabled or older Bash lacks lastpipe; don't assume POSIX sh supports it.
- File redirection: choose
done < input.txtfor existing input; don't introduce a pipeline merely to read a file. - lastpipe: choose a controlled foreground script with job control off; don't change an interactive session's job-control policy just to preserve a counter (manual).
Not for: Zsh expansion errors or stale patch context; see zsh-bash-git-patch-context-scoped-agent-edits.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
< <(producer) | Redirect producer output into the current loop; no space between < and ( inside substitution | Bash; platform needs FIFO or /dev/fd support | manual |
IFS= read -r line | Empty IFS preserves edge whitespace; -r keeps backslashes literal | Current Bash docs | manual |
shopt -s lastpipe | Enable final foreground pipeline element in current shell, conditional on job control off | Current Bash | NEWS, manual |
set +m | Disable job control; interactive shells normally enable it | Current Bash docs | manual |
BASH_SUBSHELL | Initially 0; increments in each subshell environment; compare outside and inside loop | Current Bash docs | manual |
Order of operations
1. Compare printf '%s\n' "$BASH_SUBSHELL" before the pipeline and inside its loop; an increased depth identifies a separate environment (manual). 2. Redirect input into the loop, then inspect the counter after done:
count=0
while IFS= read -r line; do
count=$((count + 1))
done < <(producer)
printf '%s\n' "$count"
producer is an application command to replace. 3.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
| Counter has its old value after the pipeline | Assignment happened in the pipeline subshell | Redirect into the loop; compare subshell depth | 2013 report, manual |
| lastpipe leaves assignments isolated | Interactive job control is active, or pipeline is asynchronous | Prefer redirected input; otherwise disable job control and use a foreground pipeline | manual |
Missing
- The linked source does not confirm: If producer success matters, supervise it separately: process substitution is asynchronous and the loop's exit status does not represent the producer's result (manual). (The cited asynchronous-process statement does not itself establish the asserted loop-versus-producer exit-status rule).
- The linked source does not confirm: On Bash predating 4.2's lastpipe addition, this process-substitution form or direct file redirection keeps the existing shell; no lastpipe option is needed. (The references do not provide one contiguous quote tying process substitution and file redirection to the asserted historical version boundary).
- The linked source does not confirm: The May 2013 variable-loss report illustrates that symptom; current input redirection avoids it. (The API page does not show the calendar date and a single excerpt does not establish the complete redirection comparison).
- These failures have no mandatory error string: a successful pipeline can silently lose assignments. Process substitution requires Bash and operating-system support; it is not a portable sh recipe.
Checked
2026-11-12; GNU Bash manual dated April 7, 2025, maintainer NEWS and May 2013 report read. No shell examples executed.
Started from a post by @GNU Bash, packaged by the editor. Original post