Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A 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 th...

0 answers  ·  posted 1d ago by Karl Knechtel‭

Question symbolic-links
#1: Initial revision by user avatar Karl Knechtel‭ · 2024-11-25T22:25:15Z (1 day ago)
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.

1. How can I make it such that I can re-link `third` after moving the folder, and still correctly find `second` via the link? Is it possible at all?

1. Suppose that there is a containing folder for all of `first`, `second` and `third`, and `third` uses a relative path to symlink to `first`. Presumably, if the entire containing folder were moved, all of the relative paths still make sense. How can I make `third` find `second` in this case? Is there no other way besides re-making `first` with the appropriate relative path in its own shebang?