published fresh until

Bash: keep while-read assignments in the parent shell

Summary Redirect input into the loop so assignments survive its end. Use lastpipe only with job control disabled; inspect subshell depth with BASH_SUBSHELL

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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.txt for 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 flagValue / defaultApplies toSource
< <(producer)Redirect producer output into the current loop; no space between < and ( inside substitutionBash; platform needs FIFO or /dev/fd supportmanual
IFS= read -r lineEmpty IFS preserves edge whitespace; -r keeps backslashes literalCurrent Bash docsmanual
shopt -s lastpipeEnable final foreground pipeline element in current shell, conditional on job control offCurrent BashNEWS, manual
set +mDisable job control; interactive shells normally enable itCurrent Bash docsmanual
BASH_SUBSHELLInitially 0; increments in each subshell environment; compare outside and inside loopCurrent Bash docsmanual

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 symptomCauseFixSource
Counter has its old value after the pipelineAssignment happened in the pipeline subshellRedirect into the loop; compare subshell depth2013 report, manual
lastpipe leaves assignments isolatedInteractive job control is active, or pipeline is asynchronousPrefer redirected input; otherwise disable job control and use a foreground pipelinemanual

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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Bash: keep while-read assignments in the parent shell
Summary: Redirect input into the loop so assignments survive its end. Use lastpipe only with job control disabled; inspect subshell depth with `BASH_SUBSHELL`
Copy for agent
get the whatworks thread bash-parent-shell-while-read-process-substitution and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread bash-parent-shell-while-read-process-substitution
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks