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")'
, where 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 bashrc:
#!/bin/bash
HERE=$(cd `dirname $BASH_SOURCE`; pwd)
PATH="${HERE}/bin:$PATH"
export HERE
export PATH
, 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:
6a7
> HERE=/tmp
15c16
< PATH=/Users/chbrandt/bin/links:/Users/chbrandt/bin/links:/Users/chbrandt/bin/links:/opt/miniconda3/bin:/opt/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
---
> PATH=/tmp/bin:/Users/chbrandt/bin/links:/Users/chbrandt/bin/links:/Users/chbrandt/bin/links:/opt/miniconda3/bin:/opt/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
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 ;)
bashshell, then start thezshshell.. <(awk '{gsub(/\${BASH_SOURCE\[0\]}/, FILENAME); print}' /path/to/the/bash/script)