How do I keep functions/variables local to my zshrc?

Dean picture Dean · Dec 2, 2012 · Viewed 20.4k times · Source

Any variable that I declare in my zshrc is available in the shell as an environment variable. I don't want this to happen.

I tried putting the variables in a function and setting them as local, but then the function is available outside of the zshrc.

How can I make it so what happens in my zshrc stays in my zshrc?

Answer

qqx picture qqx · Dec 2, 2012

If you're using a recent version of zsh you can use an anonymous function:

function () {
  local xyz=abc
  # whatever
}

The function will be automatically executed and then thrown away, it exists only for scoping purposes.

This works for any sourced file, not only zshrc.