[Shell] Shebang `#!/bin/bash` vs. `#!/usr/bin/env bash`
kawofong opened this issue · comments
Hi friends,
On the Shell Style Guide, there is strong language around how bash shebangs should be implemented: "Executables must start with #!/bin/bash
..."
However, from past experience and research online, a lot of developers in the community has suggested that #!/usr/bin/env bash
is a better shebang for portability reasons. Here are just a few of those resources:
- https://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang
- https://www.cyberciti.biz/tips/finding-bash-perl-python-portably-using-env.html
- https://stackoverflow.com/questions/21612980/why-is-usr-bin-env-bash-superior-to-bin-bash
Ask: Can someone help me understand why the shell style guide recommends such strongly on #!/bin/bash
vs. #!/usr/bin/env bash
? In my opinion, it will be better to recommend different options of shebangs and highlight their tradeoffs in the style guide instead.
Shell scripts at Google (and most companies) tend to be targeting a specific environment. Portability to BSD, Solaris, Mac etc are usually not a concern. So long as you are planning on sticking to GNU Linux, /bin/bash
tends to work and is more straight forward (and supports passing flags on the shebang line).
If you are concerned about portability, the guidance about shebangs here may not be right for you.