Ubuntu – What type of path in shebang is more preferable

scriptsshebang

In scripts, first line should specify the path to interpreter.
But on different servers Linux, Unix, or BSD this path could be different.

What is more preferable?

#!/usr/bin/env bash 

or

#!/bin/bash 

Best Answer

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.