Ubuntu: Starting bash from /usr/bin/env or /bin [duplicate]



Question:

This question already has an answer here:

Which would be advised as the first line in a bash script:

#! /usr/bin/env bash  

or

#! /bin/bash  


Solution:1

As a general rule, using env is more portable. It is unlikely but theoretically conceivable to find a system that has no bash at /bin/bash. It is also possible that the user running the script has their own bash version that is somehow different.

Using env means that the first bash instance in the user's path will be chosen. This means that #!/usr/bin/env bash will always work with the user's chosen bash version and it should always evaluate to an available bash even in non-standard systems. This becomes much more important if you are using another interpreter, not bash. Perl for example, can be installed to different locations on different systems but env will find it regardless.

So, using #!/usr/bin/env PROGRAM is a good habit to get into since it makes your scripts more portable to different *nix versions.


Solution:2

If you have serveral versions of bash installed(possible), using #!/usr/bin/env bash will ensure that the bash used is the first one on your environment's $PATH.

Using #!/bin/bash will be hardcoding the binary stored in #!/bin.

Refer: Similar question on SO


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »