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

Re: ksh compatibility: initial value of $_



> 2023/03/23 19:40, I wrote:
> 
>> 2023/01/28 10:52, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>> 
>> Src/init.c explicitly initializes $_ to the empty string.
>> 
>> Any reason not to change this?
> 
> No reason, I think, but I don't know any portable way to obtain
> the full path name of the current zsh executable.

$_ need to be initialized to the full pathname of either
current zsh executable, or the script file.
I have some difficulty with both.

[1] full pathname of the zsh executable:

On Linux we can use /proc/self/exe.

By some Googling I've found that Solaris, NetBSD, and DragonFly have
similar symlinks in /proc. On macOS we can use _NSGetExecutablePath().

On FreeBSD, it seems porcfs is obsolete.
On OpenBSD there seems to be no reliable way?

But, for {Free,Open}BSD just guessing from argv[0] and $PATH or $PWD
would be enough, although argv[0] can be set to arbitray value in
execve(2).


[2] full pathname of the script file

We can guess it from argv[1] (after removing options) and $PWD, and the
variable 'runscript' is set to argv[1] at line 290 in init.c.

But, suppose we have a ksh script 'foo' in the current directory:

% cat foo
#!/usr/bin/ksh
echo $_
% ./foo
/home/takimoto/tmp/./foo
% ksh ./foo
/usr/bin/ksh

How can we distinguish these two cases? In both cases runscript is
'./foo', I think. Probably there is a simple way that I just don't know.




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