Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

#! /usr/bin/env bash

or

#! /bin/bash
share|improve this question
1  
Unless you are using bash specific features, I'd recommend #!/bin/sh. Otherwise, just use #!/bin/bash, the alternative is for interpreters which could be installed on a non-standard path. –  edwin Apr 5 at 12:01
    
@edwin That doesn't answer my question. Which would I then use? #! /usr/bin/env sh or #! /bin/sh? Why? –  Mausy5043 Apr 5 at 12:16
    
In Unix-like systems /bin/sh is kind of a standard... similarly for /bin/bash. Well, at least in most Linux distros, also notice that #! /usr/bin/env bash is 8 characters too many :) –  edwin Apr 5 at 19:59
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.