I learned today that Bash does not support lexical scoping like I'd thought. This means that a function automatically inherits the scope of whatever calls it. I've been operating under the assumption that functions were pure, and *only* had access to arguments that are passed to it.

I am shook. I've been trying to write "functional" Bash, and this basically makes that impossible.

Zsh _does_ have some lexical scoping support. If you declare a variable as `private`, it will not be passed to functions called in that scope.

So now I'm considering rewriting all my darn scripts in Zsh. The biggest downside though is that I'll also have to find alternatives for linting and formatting. Neither shellcheck nor shfmt support Zsh. :(

Follow

@hisaac what's your take on this?

j3s.sh/thought/write-posix-she

Basically "shell is great, but if you love shell, stick to the POSIX spec and a lot of problems that make shell scripting not great will go away"

But also "only use shell for things that its good at. For everything else, there's python3 and its stdlib (or whatever else floats your boat)"

@forestjohnson I think the points are valid! I’ve mostly used Bash for my scripting — haven’t tried to do pure posix — but what they say here makes sense to me.

We’ve got a whole mess of scripts where I work (that I built), so right now I’m trying to find ways to tame the chaos until we convert to some other language.

Sign in to participate in the conversation
Pixietown

Small server part of the pixie.town infrastructure. Registration is closed.