r/bash • • 2d 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.

18 Upvotes

11 comments sorted by

7

u/roxalu 1d ago

One of the advantages of bash is its age: Many others have stepped into every trap that exist in bash - so once you know where bash pitfalls are documented you can avoid them quicker. See e.g. the urls linked to r/bash

3

u/jjangg96 1d ago

two ways out besides what you did. shopt -s lastpipe makes the last element of a pipeline run in the current shell, so the while loop keeps its variables. needs bash 4.2+ and job control off, so it works in a script but does nothing in an interactive shell.

the other is a here-string, done <<< "$(cmd)", which also keeps the loop in the current shell. it buffers all the output first though, process substitution streams it.

0

u/stianhoiland 2d ago edited 1d ago

Oh yeah, I struggled with this too. Even worse, `local` variables are also in scope of functions you call from the function declaring the local variables!

2

u/nekokattt 1d ago

local isnt isolation, it is scope layering

it is a bit confusing if comparing to how other languages operate, i agree, but it helps to consider it explicitly that way

2

u/mpersico 2d ago

No, that’s the point of local: keep the variables out of the global namespace.

4

u/Temporary_Pie2733 2d ago

Yes, but if you refer to a free variable in your function, the lookup occurs in the scope of the function that called you (dynamic scoping), rather than in the global scope, whether or not the caller used local. Local only ensures that you don’t set or overwrite a global variable, not that functions you call can’t see your local variable. 

1

u/stianhoiland 1d ago

👍🏻

1

u/mpersico 1d ago

And the problem is? If I said a variable and I call something, I want the thing I call to have my setting

2

u/Temporary_Pie2733 1d ago edited 1d ago

I don’t know what you mean by that, but unlike most languages that are statically scoped, bash is dynamically scoped, and it doesn’t have nested scopes. I suspect OP is seeing effects from those design decisions, not just the behavior of local.

Consider the function bar() { x=3; }. If you execute bar, in what scope is x set to 3? It depends on who calls bar. If you call foo() { local x; bar; }, then bar sets the variable local to foo. If you call baz() { bar; }, then bar sets the global variable x.

bash-5.2$ x=5
bash-5.2$ foo
bash-5.2$ echo $x
5
bash-5.2$ baz
bash-5.2$ echo $x
3

1

u/stianhoiland 1d ago

Ahh, to be young and naive again.

1

u/_yrlf 1d ago

I learned this recently. It's crazy that local is basically "call stack scoping" inatead of lexical scoping.