Must the shebang header always match the interpreter's installation directory? If so, then why do both
#!/usr/local/bin/python work for me?
As others have noted, the shebang line must refer to an actual file that exists.
Since different systems can have binaries installed to different locations, this is a weakness of script portability.
One way to solve this is with links, as others have mentioned.
Another way is to edit the script to refer to the right path on your system.
One more way to get around this is to use
/usr/bin/env, which will find the executable in your PATH.
So instead of:
This way, people just have to symlink
/usr/bin/env if needed, rather than every individual executable.
Of course, the executable still has to be in your PATH for this to work...
As you can see, there is no 100% clean solution. Remember: "Worse is Better" (:
#! (shebang) line must specify an executable that actually exists. If both work on your system, it means you have a python installed in both places. Or, possibly, a symlink.
Normally, you'd get an error attempted to use an interpreter that doesn't exist:
anthony@Zia:~$ /tmp/test bash: /tmp/test: /usr/local/bin/python: bad interpreter: No such file or directory
If you instead run the script like this, then the path likely doesn't matter:
anthony@Zia:~$ python /tmp/test anthony@Zia:~$
That's because you're running python directly, and passing it an argument. Python then decides to open that file and treat it as a script. In the first case, the kernel is attempting to run the script as an executable. It checks it, notices shebang line, and attempts to convert it to the second form, so it can actually run it. That fails if the path given after
#! doesn't actually exist.
Shebang is a combination of the following characters:
#! when it appears in the first line of the script. The following string is always the path to the interpreter. I'd check what exactly
/usr/local/bin/python are. It's possible that one of them is a symlink into the other. Could you please, provide the output of the following commands:
ls -l /usr/bin/python
ls -l /usr/local/bin/python