What type of path in shebang is more preferable?

If you want to use the system-installed version of a given interpreter that's installed in a standard location, use the direct path. If you want to use whatever version of the interpreter appears first in the user's $PATH, use #!/usr/bin/env ....

The env command invokes a specified command, letting you set or unset environment variables:

env FOO=BAR do-something
env DISPLAY=:0.0 xterm -ls &

If you don't specify any environment variables or other options, it will just invoke the named command. (Using it this way is arguably a bit of a hack.)

The purpose of writing the shebang as

#!/usr/bin/env interp

is to invoke whatever interp appears first in $PATH.

This means you don't have to know, when writing the script, exactly where interp is (say, if it could be in either /bin, /usr/bin, or /usr/local/bin). Of course you do have to know that env is /usr/bin/env, but that seems to be reasonably universal.

The advantage is that it invokes whichever version of the interpreter appears first in the user's $PATH. The disadvantage is that it invokes whichever version of the interpreter appears first in the user's $PATH.

For example, suppose I've installed a personal build of perl under my home directory, as $HOME/bin/perl, and I have $HOME/bin at the front of my $PATH. If I run a script whose shebang is

#!/usr/bin/env perl

then it's going to be run with my own installed perl executable -- which might not be a good thing. The author of the script probably hasn't tested it with the bleading-edge Perl that I built from source a month ago.

For something like Perl or Bash that's likely to be installed in a consistent location on most systems (/usr/bin/perl and /bin/bash, respectively), I'd use the direct path to the command. For something more obscure that could be installed differently on different systems, I'd either use the /usr/bin/env trick, or I'd write an installer that adjusts the shebang line as the script is being installed. (I used to have to do that for my Perl scripts.)

UPDATE : I've gone into a bit more detail in this answer to this question on the Unix & Linux site.


The best practice is this:

#!/usr/bin/env bash 
#!/usr/bin/env sh
#!/usr/bin/env python

And so on...

When Ubuntu first started using dash, some scripts broke. There was discussion about it. Most scripts were written #!/bin/sh which was a link to /bin/bash. The consensus is this: the script writer is responsible for specifying the interpreter. Therefore, if your script should always be invoked with BASH, specify it from the environment. This saves you having to guess the path, which is different on various Unix/Linux systems. In addition, it will work if tomorrow /bin/sh becomes a link to some other shell like /bin/wthsh or some other nonsence.