Both OSH and zsh behave that way by default, which was tangential to a point in the latest release notes https://news.ycombinator.com/item?id=29292187
Another trick I've seen in POSIX shell is to add a subshell after the pipeline until the last time you want to read the variable. Like
cat foo.txt | ( while read line; do f=$line done echo "we're still in the subshell f=$f" )
Both OSH and zsh behave that way by default, which was tangential to a point in the latest release notes https://news.ycombinator.com/item?id=29292187
Another trick I've seen in POSIX shell is to add a subshell after the pipeline until the last time you want to read the variable. Like