Which of the above forms is "better" for running bash, python etc. scripts? Why can't I just do #!$(which foo)? Is it neccecery to specify full path to env? I gather from this answer, that the path /usr/bin/env is set in stone for all *nix-es, much more than /usr/bin/python3 for example. Is that so?
Asked
Active
Viewed 596 times
0
Jeff Schaller
- 67,283
- 35
- 116
- 255
Vorac
- 3,077
1 Answers
2
You can’t just write #!$(which foo) because that line is interpreted by the kernel, which does not understand complex syntaxes like $().
The kernel does not search for the command in the PATH environment variable. That’s why you have to specify the full path to the command.
The use of /usr/bin/env is a clever hack used to search the command in the PATH. Even if there is a /usr/bin/python3 program, you may for example have installed a more recent version of Python in a different path, for example in you home directory.
user2233709
- 1,669
$()is a shell feature. As well as searching the path, consequently I can't just write#!python3. What about the cross-platform issue. Do we get/usr/bin/envon MacOS? FreeBSD? QNX? – Vorac Nov 19 '17 at 11:03PATHis not exactly a shell feature; it is alibcfeature. That’s only a guess, but/usr/bin/envmight be required by the POSIX standard. – user2233709 Nov 19 '17 at 11:13