Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Feature request: Provide name of file being sourced, to the script.
- X-seq: zsh-workers 34881
- From: Ronald Fischer <ynnor@xxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Feature request: Provide name of file being sourced, to the script.
- Date: Mon, 13 Apr 2015 16:51:46 +0200
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=mm.st; h= content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=quM 8orxdSvX+zk3rkm4x98npHVY=; b=coHOujIH9zGfDEjisMbjPTiT6C2CGLjmyAT hv9StfP7TUvdRGVKFqne2zBWP5vWcBHkVIBdoYCpG8dkKQQYqYo+uNpbVgX/ohrD ifItOGLqyqhHX0AsARjk4uIlFhfwbmMm+hsAZdpIl3Ix7c626KxZwM6RoQ4ApJDJ XHrOLkeY=
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=quM8orxdSvX+zk3rkm4x98npHVY=; b=HFlDY fxtFfHBsWkVvxTtLjCYt0jURSurbaN8wd6Hlw8qdB9lWLqaO5DT9McqGplVvh1ko cEhXglfkn+8zxkr0NfuW1VEDyHRW9IhNyuFWABkTgA/a+a/v28OW8NEoyEFsmklh UujMDTBDpdnA68iCUUbN2vxPD9Ao6h/8Z8piNg=
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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