Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to know whether it is good practice, according to you, to use this she-bang:

#!/usr/bin/env bash

instead of

#!/bin/bash

This article seems to advocate for the use of /usr/bin/env but when I use it, skepticism appears.

What is your opinion?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

In practice, #!/usr/bin/env is portable to almost every system that understands #!. It's a useful idiom for invoking a program that could be anywhere in $PATH. If you hard-code the path to the interpreter, your script might not work if you copy it to a different machine.

My general policy is to use #!/bin/sh for scripts that only use portable sh features (i.e., only POSIX features if I only care about modern unices, and only Bourne features if I care about older systems such as older Solaris releases) and #!/usr/bin/env otherwise.

A downside of #!/usr/bin/env is that it doesn't let you pass any option to the interpreter. With some interpreters, this can be worked around by specifying the options as directives instead, or by making the second line be a dual-language snippet that invokes the interpreter when executed by sh and does nothing when executed by the target interpreter. There's a common perl idiom, for instance (useful to pass -T).

share|improve this answer

env is only useful if the script is going to be used on multiple systems where the location of the binaries can vary. If the script is only used on a single system or in a homogeneous network then it's not necessary.

share|improve this answer

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.