Track and use the "function name" for $0 in forked binaries #194
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Very picky, very niche change. Found while thinking about
path-cache.es
and a notion of "aliases" that I've been playing around with.Here is a script which demonstrates the change.
At head this script prints (on my machine):
In this fork this script prints:
There are two effects going on here:
fn-name = /path/to/bin
, then runningname
will invoke/path/to/bin
with$0
set toname
.%pathsearch
such that%pathsearch name
returns/path/to/bin some args
, then runningname
will invoke/path/to/bin some args
with$0
set toname
.What's the use of this? Well, it makes having a "reasonable" value for
$0
possible in more cases. For example, when usingpath-cache.es
, at HEAD we get:which is, IMO, less than ideal. With this change, it prints
bash
every time. In theory we could just modifypath-cache.es
to definefn-bash = %run /usr/bin/bash bash
instead of what we have now, but that feels to me less like letting the user define the behavior they want and more like working around a defective default.The part of the change to do with
%pathsearch
is a little more sketchy, since es doesn't actually make any promises about what should happen when your%pathsearch
receives multiple arguments or returns values other than a single binary. But for something like my trial "aliasing" setup here:this change allows the user to keep the
$0
they typed into their terminal or script. (Technically the behavior above differs from other shells; in this setupegrep
will be the$0
, while other shells will usegrep
. Is that a dealbreaker?)All this behavior here is a little subtle. For example, when we have
fn ls {/bin/ls $*}
, runningls
invokes the binary with$0
as/bin/ls
, notls
, because technicallyls
doesn't refer to/bin/ls
but rather a lambda which just so happens to run/bin/ls
. But I think it provides a fairly good balance of intuition in the simple case and control in the more complex one.