1

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

#! /usr/bin/env bash

or

#! /bin/bash
3
  • 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, 2014 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, 2014 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, 2014 at 19:59

2 Answers 2

3

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.

1
  • If /bin/bash doesn't exist, are we any more sure that /usr/bin/env will? For bash, specifically, I generally just use /bin/bash, because if I had to bet on one, I pick that over /usr/bin/env. For almost everything else, though, I use /usr/bin/env Oct 20, 2021 at 20:57
1

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

Not the answer you're looking for? Browse other questions tagged .