I have defined some variables, aliases and functions in my .zshrc file:
export MY_VAR="example"
alias my_alias="echo an example"
function say_hello
{
echo "say hello"
}
I have verified that all three are defined and whatnot when called from the terminal. However, when I try to call the function say_hello from another script (which itself is called from the same terminal), the function does not appear to be defined. I see a ‘command not found’ error. The alias and variable MY_VAR appear to be defined just fine when referenced from this other script.
Any ideas on what might be going on? Thanks.
When zsh is called from a terminal it is invoked in interactive mode that causes zsh to source additional configuration files, including
$ZDOTDIR/.zshrc($HOME/.zshrc). By default (in non-interactive mode in non-login shell) it sources only/etc/zsh/zshenv* and$ZDOTDIR/.zshenv($ZDOTDIRis most of time$HOME) files, see last but two section ofman zshnamedSTARTUP/SHUTDOWN FILES. I have no idea why alias is defined (how exactly did you check?), but MY_VAR is defined in script because you exported it.*
/etc/zshenvaccording to doc, much likely they are Gentoo maintainers of zsh package who changed it to/etc/zsh/zshenv.Note: it is bad idea to put such functions into
.zshenvfile as it creates implicit dependency. You should use script libraries instead: put it intoand do
in both your script and
.zshrc.