Which would be advised as the first line in a bash script:
#! /usr/bin/env bash
or
#! /bin/bash
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.
/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
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
#!/bin/sh
. Otherwise, just use#!/bin/bash
, the alternative is for interpreters which could be installed on a non-standard path.#! /usr/bin/env sh
or#! /bin/sh
? Why?/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 :)