Scripts do not necessarily have a shebang
If the script was run from the interpreter, You cannot be sure it has the shebang at all. Scripts, run from the interpreter do not need the shebang, if you call the interpreter to run the code.
The answer is therefore no, there is no command that will find out for sure what is the language (interpreter) to run the script with. You can however always look inside the script and see if it has the shebang to find out.
The rules in short:
- When you run the script, calling the interpreter always overrules possible shebangs, executable or not, shebang or not.
- If not executable and run from the interpreter, the script needs no shebang.
- If the script is run without calling the interpreter first, it needs (and uses) the shebang to find out what interpreter to call, and it needs to be executable to have the "permission" to call the interpreter from its shebang.
If the script has no shebang however, there is no (direct*) information inside the script to tell what interpreter to use.
Having said that
You could of course always write a wrapper script to try to find out if the script has the shebang and read the interpreter from that, subsequently run it from the found interpreter.
An example
#!/usr/bin/env python3
import subprocess
import sys
args = sys.argv[1:]; script = args[0]
try:
lang = open(script).readlines()[0].replace("#!", "").strip().split()[-1]
cmd = [lang, script]+args[1:]
subprocess.call(cmd)
except (PermissionError, FileNotFoundError, IndexError):
print("No valid shebang found")
Save it as tryrun
in $PATH
(e.g. ~/bin
, make the directory if it does not exist, log out and back in), make it executable. Then running:
tryrun /path/to/nonexecutablescript
calls (tested) the correct interpreter on my non-executable python
and bash
scripts.
Explanation
- The script simply reads the first line of the script, removes the
#!
and uses the rest to call the interpreter.
- If it fails to call a valid interpreter, it will raise either a
PermissionError
or a FileNotFoundError
.
Note
The extension (.sh
, .py
etc) plays no role whatsoever in determining the appropriate interpreter on Linux.
(*It is of course possible to develop a "smart" guess- algorithm to determine the syntax from the code.)
bash whatever
works, why use something different? bash is available on virtually every *ix system, so why bother... – steffen Nov 19 '16 at 12:05