What is the difference between /bin/bash and /usr/bin/env bash?

Asked

Viewed 5,857 times

14

I have not much knowledge of shell script, always used /bin/bash, but I noticed other variations:

  1. #!/usr/bin/env bash

  2. #!/usr/bin/bash

  3. #!/bin/bash

What’s the difference between them?

As far as I read this is due to portability between different systems (or kernels), would like to know how to call declare the script type in a way that works on Unix-like systems, such as systems based on Linux, Mac OS X and systems based on BSD.

1 answer

16


First of all, the #! in a shell script is known as shebang or hashbang. It has the shape of a comment (lines started with # are usually ignored by the shell), but the ! then serves to indicate which is the executable that will process that script.

When you say:

#!/usr/bin/bash

or

#!/bin/bash

is calling the executable bash to interpret your script. It could be python or another interpreter.

The problem is that of distro for distro, the place of bash (or of python, for example) may be different. When you do so:

#!/usr/bin/env bash

is executing the command env with the parameter bash. env serves to create a new environment, and the following parameter is the command that will be executed by env in this environment.

Like the env uses the path of the system, the bash will run without you having to set your exact path.

This article summarizes this (en):

http://www.cyberciti.biz/tips/finding-bash-perl-python-portably-using-env.html


Now it remains to be seen whether it is really true that the env will always be in /usr/bin :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.