What is the difference if I start bash with “/bin/bash” or “/usr/bin/env bash”?

In shell scripts one specifies language interpreter on shebang(#!) line. As far as I know, it is recommended to use #!/usr/bin/env bash because env is always located in /usr/bin directory while location of bash may vary from system to system. However, are there any technical differences if bash is started directly with /bin/bash or through env utility? In addition, am I correct that if I do not specify any variables for env, the bash is started in unmodified environment?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In one sense, using env could be considered “portable” in that the path to bash is not relevant (/bin/bash, /usr/bin/bash, /usr/local/bin/bash, ~/bin/bash, or whatever path) because it is specified in the environment. In this way, a script author could make his script easier to run on many different systems.

In another sense, using env to find bash or any other shell or command interpreter is considered a security risk because an unknown binary (malware) might be used to execute the script. In these environments, and sometimes by managerial policy, the path is specified explicitly with a full path: #!/bin/bash.

In general, use env unless you know you are writing in one of these environments that scrutinize the minute details of risk.

When Ubuntu first started using dash, some time in 2011, many scripts were broken by that action. There was discussion about it on askubuntu.com. Most scripts were written #!/bin/sh which was a link to /bin/bash. The consensus was this: the script writer is responsible for specifying the interpreter. Therefore, if your script should always be invoked with BASH, specify it from the environment. This saves you having to guess the path, which is different on various Unix/Linux systems. In addition, it will work if tomorrow /bin/sh becomes a link to some other shell like /bin/newsh.

Another difference is that the env method won’t allow the passing of arguments to the interpreter.

Method 2

Apart from that using /usr/bin/env is somewhat slower there is no difference, if you start a program which such a shebang. (Unless /bin/bash doesn’t exists but bash is somewhere (else) in the PATH)

env has the possibility to modify the environment of the invoked command, but that can only be used when env is started from the commandline, with in shebang line (depending on the OS) the options cannot be specified.

The source of env is rather small so you can verify what it is doing if you are familiar with C. Since execvp is used to call the program to be executed. There will not even be an env parent process if you inspect the process tree.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x