Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In the header of a Bash script, what's the difference between those two statements:

  1. #!/usr/bin/env bash

  2. #!/usr/bin/bash

When I consulted the env man page, I get this definition:

 env - run a program in a modified environment 

What does it mean?

like image 551
Salah Eddine Taouririt Avatar asked May 03 '13 18:05

Salah Eddine Taouririt


People also ask

What is the definition of the difference between?

The difference between two things is the way in which they are unlike each other.

What is difference between among and between?

The most common use for among is when something is in or with a group of a few, several, or many things. The most common use of between is when something is in the middle of two things or two groups of things. It is sometimes used in the phrase in between.

What is the difference between difference and differentiation?

To differ means to be different. To differentiate means to make (someone or something) different in some way.


2 Answers

Running a command through /usr/bin/env has the benefit of looking for whatever the default version of the program is in your current environment.

This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your path, it will find it.

One downside is that you will be unable to pass more than one argument (e.g. you will be unable to write /usr/bin/env awk -f) if you wish to support Linux, as POSIX is vague on how the line is to be interpreted, and Linux interprets everything after the first space to denote a single argument. You can use /usr/bin/env -S on some versions of env to get around this, but then the script will become even less portable and break on fairly recent systems (e.g. even Ubuntu 16.04 if not later).

Another downside is that since you aren't calling an explicit executable, it's got the potential for mistakes, and on multiuser systems security problems (if someone managed to get their executable called bash in your path, for example).

#!/usr/bin/env bash #lends you some flexibility on different systems #!/usr/bin/bash     #gives you explicit control on a given system of what executable is called 

In some situations, the first may be preferred (like running python scripts with multiple versions of python, without having to rework the executable line). But in situations where security is the focus, the latter would be preferred, as it limits code injection possibilities.

like image 107
Alec Bennett Avatar answered Oct 04 '22 17:10

Alec Bennett


Using #!/usr/bin/env NAME makes the shell search for the first match of NAME in the $PATH environment variable. It can be useful if you aren't aware of the absolute path or don't want to search for it.

like image 41
Dolphiniac Avatar answered Oct 04 '22 17:10

Dolphiniac