zsh-workers
 help / color / mirror / code / Atom feed
From: Ronald Fischer <ynnor@mm.st>
To: zsh-workers@zsh.org
Subject: Feature request: Provide name of file being sourced, to the script.
Date: Mon, 13 Apr 2015 16:51:46 +0200	[thread overview]
Message-ID: <1428936706.1922179.253003181.2E00EEA4@webmail.messagingengine.com> (raw)

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.


  


             reply	other threads:[~2015-04-13 14:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-13 14:51 Ronald Fischer [this message]
2015-04-13 15:24 ` Eric Cook
2015-04-13 15:39 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1428936706.1922179.253003181.2E00EEA4@webmail.messagingengine.com \
    --to=ynnor@mm.st \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).