Re: full path of a script



2007-09-25, 15:01(+02), Joachim Schmitz:
[...]
~/bin$ print '#! /bin/bash -\nprintf "%s\\n" "$0"' > a
~/bin$ chmod +x a
~/bin$ PATH= /bin/bash -c a
a
~/bin$ PATH=. /bin/bash -c a
./a
Here too... and no contradiction to what I said. Or meant to say... if bash
finds it somewhere in PATH, $0 contains an absolute pathname

First both (except for some old bogus versions of ksh) only
lookup the command in $PATH if the script name passed as
argument doesn't contain any slash and is not to be found in the
current directory. That's why I said it can only happen if you
call the shell interpreter directly as in "bash a" or "ksh a".

In that case, I find that both bash and ksh behave the same:

~$ PATH=~/bin /bin/ksh a
a
~$ PATH=~/bin /bin/bash a
a

, if it is in
the current dir and that is part of PATH, it prints a relative name
ksh behaves different, here $0 expands to a relative path if it is found
somewhere in PATH and to a plain filename if it's found in the current
directory
[...]

I still don't see what you mean. I know some old versions of ksh
had a bug where it would search in $PATH before searching in the
current directory. Is that what you are refering to?

If not, could you please give an example?

[...]
You're right, I've looked it up in the source code, it's well hidden (and I
missed it the first time), but it's indeed there... It doesn't seem
documented though
[...]

I beleive it is. And it's also how it is specified by POSIX, cd
is a short for "cd -L", same for pwd. In scripts using -L almost
never makes sense, so systematically adding -P is generally a
good idea.


--
Stéphane
.