Cool; I had a similar issue -- which I solved by other means, after giving it a second thought --, but this question is quite interesting actually. And the solution quite elegant (I think so).
The main/real question (AFAIU) is:
I need to source it from a zsh shell as I need to have all the environment variables it defines.
, where it is the Bash script.
The fact that the script uses
BASH_SOURCE is actually secondary; The script is Bash, period. We may be consider it a "black-box", and all we wanna do is to figure out what variables are being defined (or modified) there.
- The script has to be source'd on Bash.
That being said, what we have to do is to (from inside a Bash interpreter)
- buffer the initial state of the environment,
- source the script,
- buffer the afterwards state of the environment,
- diff the buffers.
And you/we should have the new/modified variables printed out.
The proposed SOLUTION -- the above steps -- look like:
% bash -c 'buf1=$(env|sort) && source bashrc && buf2=$(env|sort) && diff <(echo "$buf1") <(echo "$buf2")'
bashrc is the name of the supposed script.
Notice that this actually works for any shell (zsh, csh, ksh, etc), as long as Bash is installed and the version is compatible with the script (most likely to happen, but worth noting).
As an EXAMPLE, I have here the following script -- I'm calling it
HERE=$(cd `dirname $BASH_SOURCE`; pwd)
, which defines a new variable --
$HERE -- and a modifies an existing one --
$PATH. I put this script in my
/tmp directory. The above command gives me:
Sweet, isn't it? ...Now you do some post-proc (inside your zsh, for instance) to handle the symbols and whatever else and that's it ;)