Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Note that since you don't quote any of the expansions, your code will not cope with arguments containing spaces or filename globbing characters (under some conditions). It would also break if $0 contains spaces. Also note that re-executing the current script with bash could potentially break the script. It would be much better and safer to just write bash shell code from the start without trying to compose polyglot scripts. Treat each shell as if they were separate languages. You would not mix Perl with Ruby or Python. Commented Sep 25, 2020 at 15:02
  • So the reason I needed this, is that someone was calling all scripts as /bin/sh in an Ubuntu environment. In Ubuntu /bin/sh points to dash. So I added this to the head of my bash scripts for protection and redirection as shells do not respect the #!/bin/bash that is also there. Commented Sep 30, 2020 at 14:41
  • It feels like you're trying to come up with a technical solution to something that in essence is a social problem. Ask them to run your script with the correct interpreter, and also mention this in the documentation. Commented Sep 30, 2020 at 15:54
  • Completely is. Cannot get them to understand that sh is fluid while dash bash are fixed; and why that's important. Commented Sep 30, 2020 at 17:32
  • Maybe just tell them to invoke an sh wrapper that runs your script? I.e. split it in two: wrapper script written for sh (or whatever shell), calling the real script without explicit interpreter. Commented Sep 30, 2020 at 17:59