Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Making a script 'sourceable'



    Hi all :)

    Last month I asked here how to detect, portably, whether a script
had been sourced, and since then I've tried, with no success.

    That given, I'm trying to do two things. First one is detecting
the 'sourcing' non portably, in zsh. Checking for $SHLVL doesn't
work, because the user could have launched a new instance of zsh and
so SHLVL will appear increased even when sourceing. So what I'm
thinking about now is to check the value of the option 'interactive'.
If it is on, chances are that we have sourced the script. Otherwise,
we may not. This is not perfect, though, because you can run a script
(interactive=off) and then source the other script within...

    The second thing is derived from the above question: since
checking for 'sourcery' ;) is very difficult even non portably, I've
thought about making my zsh scripts sourceables. I thought that simply
doing 'emulate -L zsh' will do, since any change to options and
values will remain local to the script, but it won't work when
sourceing because the script is run in the current environment :(

    So I must, by hand, restore any changed variables, options and,
what more? Must I restore any other thing? Is there any simple way of
making scripts source-safe or source-aware?

    Thanks a lot in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/



Messages sorted by: Reverse Date, Date, Thread, Author