Is there a command or environment variable that I can test for in my ~/.zshrc
that that would differentiate between when I run source ~/.zshrc
to update my configuration and when the shell reads ~/.zshrc
as it's initialized?
So far my solution is to set a variable at the end of the ~/.zshrc
and test for that variable on subsequent exectuion, but I'm curious if there's a cleaner way to directly get this information from the system or envrionment rather than hacking it together in a way that feels so fragile….
# somewhere in ~/.zshrc
if [[ -n $CONSOLE_ALREADY_RUNNING ]]; then
echo "we've alredy loaded ~/.zshrc"
echo "so you must be sourcing it"
fi
# many more lines of ~/.zshrc commands
# last line of ~/.zshrc
export CONSOLE_ALREADY_RUNNING=1
Seems like there should be some cleaner way to test if I'm just reloading via source /.zshrc
or if .zshrc
is being executed for a new instance of zsh
that was just loaded into memory.
Best Answer
$ZSH_EVAL_CONTEXT
is"toplevel"
if you call the script from a shell. (for example./script
)$ZSH_EVAL_CONTEXT
is"toplevel:file"
if you source the script. (for examplesource script
)$ZSH_EVAL_CONTEXT
is"file"
if it's run automatically as a runtime configuration. (like~/.zshrc
would be.)Found the variable here.