Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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

#! /usr/bin/env bash


#! /bin/bash
share|improve this question

marked as duplicate by Community Dec 30 '15 at 20:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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 '14 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 '14 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 '14 at 19:59
up vote 2 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

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

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