question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change `#!/bin/bash` shebangs to `#!/usr/bin/env bash` for better compatibility

See original GitHub issue

Some operating systems don’t have a Bash executable located at /bin/bash (e.g. NixOS does this intentionally), so shell scripts using #!/bin/bash as their shebang won’t function without modification.

I suggest replacing #!/bin/bash with #!/usr/bin/env bash to improve compatibility with systems that don’t have /bin/bash.

I’m willing to implement this change — it’s a pretty simple change — but I’m presenting the idea first as an issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Mic92commented, Aug 2, 2020

I had some kid tell me this was a huge security vulnerability at my last job, but he never gave me a good reason why. I wonder if anyone else has an opinion…

That might be the case if you have binaries in setuid scripts (very bad idea in the first place). But than you also need to do a lot of hardening in general i.e. use absolute PATHs everyhere or set your own PATH and sanitize other environment variables. I don’t think this applies here.

1reaction
Mic92commented, Aug 2, 2020

Nixpkgs/NixOS maintainer here. It is not just NixOS and Guix. FreeBSD has bash in /usr/local/bin. MacOS bash at /bin/bash is ancient and people might want to replace it with homebrew’s bash. Also see this discussion in systemd: https://github.com/systemd/systemd/pull/5816 TL;DR upstream should rely on the developers/user’s PATH for flexibility. Linux distribution have control over their binaries during packaging and have infrastructure replace /usr/bin/env shebangs automatically with absolute paths to the right binaries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the preferred Bash shebang ("#!")?
You should use #!/usr/bin/env bash for portability: Different *nixes put bash in different places, and using /usr/bin/env is a workaround to ...
Read more >
Is there any reason to have a shebang pointing at /bin/sh ...
This means that I always use #!/bin/bash in my shebang unless I have verified that I don't have bashims in my script. By...
Read more >
What is the preferred Bash shebang ("#!")? : r/commandline
I am really confused between shebangs for Bash Script (.bash) and Shell Script (.sh). Is there any Bash shebang objectively better than the ......
Read more >
The Difference Between #!/usr/bin/bash and #!/usr/bin/env ...
#!/usr/bin/bash is a shebang line used in script files to set bash, present in the '/bin' directory, as the default shell for executing...
Read more >
Make Linux Script Portable With #!/usr/bin/env As a Shebang
Learn how to make Linux/Unix script portable with #!/usr/bin/env shebang to execute script using the interpreter such as bash, perl, python.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found