3 ms·
It's there because on some systems all the args are passed as one. -S splits them out. You can try this on Linux and env even warns you about it: # cat t
by tgamblin 6y ago
It's there because on some systems all the args are passed as one. -S splits them out. You can try this on Linux and env even warns you about it:
# cat test.py
#!/usr/bin/python3
import sys
print(sys.argv)
$ cat test
#!/usr/bin/env test.py foo bar baz
$ PATH=. ./test
/usr/bin/env: 'test.py foo bar baz': No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines
with -S, env does the right thing, and you get this:
$ cat test
#!/usr/bin/env -S test.py foo bar baz
$ PATH=. ./test
['./test.py', 'foo', 'bar', 'baz', './test']
- chrismorgan 6y agoYou completely missed the point of what I was writing. I’m not talking about why -S, but why /usr/bin/env -S.
- tgamblin 6y agoYeah I guess I read that too quickly -- sorry. This: #!python3 Seems to work (i.e., search $PATH) on macos but AFAIK nowhere else. It fails on both ubuntu20 and centos8. Per this: https://www.in-ulm.de/~mascheck/various/shebang/ https://www.in-ulm.de/~mascheck/various/shebang/, relative paths are supposed to be interpreted as relative to ., so I'm guessing that is why everyone uses /usr/bin/env. Given that shebang is implemented in the kernel (and not in user-space like coreutils) I think env is more likely to change than the shebang mechanism, though who knows -- there have been recent changes to shebang so perhaps further work on this will help: https://lwn.net/Articles/779997/ https://lwn.net/Articles/779997/.
- chrismorgan 6y agoThe shell you’re using matters, too; I find that PATH search happens when starting things from zsh, but not bash or sh. That might be why you observe it working on macOS, I dunno—they switched to zsh a while back, didn’t they? I’m not sure how much is in the kernel and how much is elsewhere, but I do observe that the shell is at least partly responsible for the error message like `bash: /path/to/script-with-shebang: python3: bad interpreter: No such file or directory`.