Simple question, is the same ? Vim autocomplete me when write bash + tab => #!/usr/bin/env bash but I usually watch #!/bin/bash in every script. So..
- 185
-
1Another similar post on Stack Overflow: Why is #!/usr/bin/env bash superior to #!/bin/bash? – Byte Commander Apr 15 '22 at 21:57
1 Answers
TL;DR:
Use #!/usr/bin/env bash
Long answer:
Nope, they are not identical. The first form (#!/usr/bin/env bash) is better, as it will work when Bash is in your path, but isn't in /bin. Often, both will work. However, if your script runs on a system where /bin/bash isn't Bash (or isn't a symlink to it), then #!/bin/bash won't work as it isn't in that location, but #!/usr/bin/env bash will.
env looks up a command in your path, so if Bash is in your path but not in /bin, then you need to use env. Practically, because a decent amount of scripts don't use env, OS's tend to make /bin/bash either Bash or a symlink to it. Note that there is a small chance env won't work if you have using some old system, but on any reasonably modern OS (Debian, Ubuntu, Arch, Fedora, etc.) it is best to use env.
- 3,894
-
1This recommendation is preposterous. You can't make recommendation that works in every case without knowing the context. For example system scripts must use
/bin/bashbecause they have to work, and they're guaranteed to work with/bin/bashwhereas the user environment may use a differnet bash version or may not have it at all. Another case is when the script is owned byrootand has the setuid bit. This case is a huge security concern when usingenvbut not a problem when using/bin/bash– Eric Oct 27 '22 at 11:05 -
The concern about setuid makes sense - it wasn’t something I considered. Consider either suggesting an edit to my answer explaining the pitfalls of doing it this way and why having the actual path can be better. If you’d prefer to write your own answer, you could on the question this is a dupe of (although check to ensure existing answers don’t include that) – cocomac Oct 28 '22 at 04:27
-