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

Feature request: Provide name of file being sourced, to the script.



While a zsh script can find out about itself (i.e. where it is located),
by looking at the variable $0, a piece of code executed by the 'source'
statement does not have this information. $0 always points to the script
which "belongs" to the zsh process.

Example: We have a script /home/usr/foo/x.zsh containing the lines

    #!/bin/zsh
    source /home/usr/bar/y.inc

we can have in /home/usr/foo/y.inc the line

    echo $0

which would output /home/usr/foo/x.zsh , but we have no way of
"knowing", that the echo command being executed is in
/home/usr/bar/y.inc

I suggest that zsh provides a shell variable, similar to $0, which
returns the path of the script being sourced. I suggest that this
variable is named __SCRIPT_PATH__ . Outside of a sourcing process, the
variable might return either the empty string, or $0; this is something
which would need yet to be decided.

Why would this be useful?

Consider a "library" of scripts, which are intended to be sourced (maybe
because the chdir to somewhere and the directory needs to be kept, or
maybe because they are supposed to set up an environment), and let's
assume that these scripts depend on each other, in that one of them
sources another one from this set. Since it is a library, we require
that all those scripts are installed in the same directory.

The problem is, that these scripts do not know the path of the
respective other scripts to be sourced, because by the time of writing
of these scripts, we don't know where they will be installed to.

If they were just "normal" scripts, not to be executed by a "source"
command, a script could simply use $0:A:h to find its own absolute path,
and located the "sibling" scripts in this directory. 

With a sourced script, this is not possible. The only solutions are:
Require the PATH variable to point to the directory containing the
scripts, and just use the script name without path component; or,
requireing the top level script (which initially sources one of the
scripts in the library), to pass as parameter its path, and have the
scripts pass on the path to each other.

Both solutions make our script library inconenient to use.


  



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