Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

zsh

zshrc

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?

like image 521
Dean Avatar asked Dec 02 '12 07:12

Dean


People also ask

What is Precmd zsh?

precmd is executed before your prompt is displayed and is often used to set values in your $PROMPT . preexec is executed between when you press enter on a command prompt but before the command is executed. Each hook is an array of widgets which are executed by zsh.

What is zsh autoload?

The autoload command essentially instructs Zsh to register a function (called clock) using the contents of the clock file that is located in a directory somewhere along the fpath.


1 Answers

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.

like image 141
qqx Avatar answered Sep 28 '22 09:09

qqx