Running an executable through a symlink, at the target's location
I have this test setup:
$ cat first
#!./second
one
two
three
$ cat second
#!/usr/bin/env python
import sys
for line in open(sys.argv[1]):
print(line, end='')
After chmod +x on the files, I can successfully run ./first from this directory and it displays its own source code. This confirms that a) relative paths work in shebangs and b) interpreters can be "chained" (i.e. the specified interpreter could itself be a script).
However, if I now create a symlink somewhere else to first, I can't run the script via the symlink - it only works from within that directory. Whether the symlink itself uses an absolute or relative path doesn't seem to matter - either way I get bash: ./third: ./second: bad interpreter: No such file or directory.
It appears that first runs with third's CWD, such that the relative path in the shebang is interpreted relative to the symlink, rather than the original file.
I understand that in general, moving the folder with first and second would break the symlink. But there are still useful things I think I could do here.
-
How can I make it such that I can re-link
thirdafter moving the folder, and still correctly findsecondvia the link? Is it possible at all? -
Suppose that there is a containing folder for all of
first,secondandthird, andthirduses a relative path to symlink tofirst. Presumably, if the entire containing folder were moved, all of the relative paths still make sense. How can I makethirdfindsecondin this case? Is there no other way besides re-makingfirstwith the appropriate relative path in its own shebang?

0 comment threads