This is a long outstanding story and there is not (unfortunately) a unique answer. What seems to be the preferred way al least is
#!/usr/bin/env bash
.
This should guarantee the maximum portability as explained in this Stack Overflow post.
For all the other languages is fairly easy: #!/usr/bin/env ruby
.
Why Should I Use #!/usr/bin/env bash
?
env
searches $PATH
for the executable named by its first argument, in this case “bash”. For a more detailed explanation of this technique and how it differs from the old #!/bin/bash
, see this question in StackOverflow.
There are roughly four levels of portability for shell scripts as far as the shebang line is concerned. An extensive explanation is available here.
Most Portable solution
Only if you have a script with something extremely simple you can use a #!/bin/sh
shebang but remember to use only the basic shell syntax specified in the POSIX standard. This should work on pretty much any POSIX/Unix/Linux system. Except Solaris 10 and earlier which had the real legacy Bourne shell, predating POSIX so non compliant with /bin/sh
😀.