Correct me if this statement is wrong: All variables/parameters that defined inside a zsh script keep its persistent scope globally in current shell session.
If above statement is true, then to avoid messing around the current shell's environment variables / other zsh scripts. Should we put all variables/parameters of a zsh script to anonymous/local inner function if possible?
The possibilities can be counted as:
- We use that variables right away in that local scope => anonymous function
- Mark their scope by using maker
always=> inner func using
- Mark but using rarely used traps to mark => inner func using traps
The question and the purpose is that all variables/parameters must local inside a zsh script and must be destroyed after used inside that zsh script.
EDIT: I did confuse about runing and sourcing shell script.
I want to
source a script inside a script but try to keep minimizing affect of the sourcing script to mess the calling script's environment.
@Stéphane Chazelas did answer the question but I'd to make it clear as I understand, correct if I'm wrong.
Running a shell script inside a shell script
./script.zsh will invoke new shell to run the script then return exit code to the calling script.
Sourcing a shell script inside a shell script
. ./script.zsh will create a sub-process of the current shell and any changes - define new variables, functions.. will be put in the current shell's environment, this is expected behavior because mostly user want to use them after sourcing process but you can exclude unwanted portions by using anonymous funcion like @Stéphane Chazelas mentioned in comment. With note that
alias always comes up with global scope.