#!python3
print("Hello")
I find that this code works fine in my terminal. But everyone does #!/path/to/file
or #!/usr/bin/env command
.
Is there any reason to avoid using #!command
in shebang lines?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityA path-less shebang assumes that the command in the shebang is in the current directory, in the general case. More generically, a non-absolute shebang is interpreted relative to the current directory of the process executing the script.
Path-less shebangs where the command isn’t in the current directory work only when the script is started from certain shells (Zsh at least, but not Bash), and it works because the shell helps out. When the script is run, execution fails, but the rule then is for the shell to try to run the script if it thinks it is a script; Zsh looks up shebang commands in its path, but that’s not standard.
Scripts with path-less shebang commands won’t work in any other context.
The idiomatic way to write a PATH
-based shebang is to use /usr/bin/env
, as you mention:
#! /usr/bin/env python3
or, with some versions of env
,
#! /usr/bin/env -S python3 --
to avoid problems with paths to the script starting with dashes.
python3 script.py
.
#!python
at the top of Python scripts on Windows, because they are sufficient for interpretation by thepy
utility.