Can a script be used as an interpreter by the #! hashbang line? Can a script be used as an interpreter by the #! hashbang line? unix unix

Can a script be used as an interpreter by the #! hashbang line?


To make this work you could add the interpreter's interpreter (i.e. bash) to the shebang:

#!/bin/bash /bin/interpreterHere are some commands for the custom interpreter.

bash will then run your interpreter with the script path in $1 as expected.


You can't use a script directly as a #! interpreter, but you can run the script indirectly via the env command using:

    #!/usr/bin/env /bin/interpreter

/usr/bin/env is itself a binary, so is a valid interpreter for #!; and /bin/interpreter can be anything you like (a script of any variety, or binary) without having to put knowledge of its own interpreter into the calling script.


Read the execve man page for your system. It dictates how scripts are launched, and it should specify that the interpreter in a hash-bang line is a binary executable.