r/bash • u/deployfan64 • 20h ago
help TIL that `local` in bash doesn't scope variables the way I assumed inside pipelines
I had a function that built a string in a while-read loop inside a pipeline, and a variable set with `local` inside the loop was coming up empty after the loop finished. Turns out each side of a pipeline runs in a subshell, so no amount of `local` helps — the assignments die with the subshell. I ended up using process substitution (`while read ... done < <(some_command)`) instead, which keeps the loop in the current shell. Cost me an hour of staring at a script that "worked" when I ran the pieces by hand.