Bem, o padrão POSIX não tem o que dizer:
One common historical implementation is that the execl(), execv(), execle(), and execve() functions return an [ENOEXEC] error for any file not recognizable as executable, including a shell script. When the execlp() and execvp() functions encounter such a file, they assume the file to be a shell script and invoke a known command interpreter to interpret such files. This is now required by POSIX.1-2008. These implementations of execvp() and execlp() only give the [ENOEXEC] error in the rare case of a problem with the command interpreter's executable file. Because of these implementations, the [ENOEXEC] error is not mentioned for execlp() or execvp(), although implementations can still give it.
Another way that some historical implementations handle shell scripts is by recognizing the first two bytes of the file as the character string "#!" and using the remainder of the first line of the file as the name of the command interpreter to execute.
One potential source of confusion noted by the standard developers is over how the contents of a process image file affect the behavior of the exec family of functions. The following is a description of the actions taken:
If the process image file is a valid executable (in a format that is executable and valid and having appropriate privileges) for this system, then the system executes the file.
If the process image file has appropriate privileges and is in a format that is executable but not valid for this system (such as a recognized binary for another architecture), then this is an error and errno is set to [EINVAL] (see later RATIONALE on [EINVAL]).
If the process image file has appropriate privileges but is not otherwise recognized:
If this is a call to execlp() or execvp(), then they invoke a command interpreter assuming that the process image file is a shell script.
If this is not a call to execlp() or execvp(), then an error occurs and errno is set to [ENOEXEC].
Você notará que ele não especifica como o interpretador de comandos em 3.1 é identificado.
Mas no Linux , é necessário que o shebang comece com #!
exatamente. Veja man 2 execve
:
execve() executes the program pointed to by filename. filename must
be either a binary executable, or a script starting with a line of
the form:
#! interpreter [optional-arg]
E #
é um caractere de comentário bastante comum, e é por isso que ##!
parece cancelar o shebang sem nenhum outro efeito. No entanto, o script ainda será executado usando sh
, portanto, mesmo se /foo/bar
não for executado, talvez não seja o esperado. Veja man 3 exec
:
If the header of a file isn't recognized (the attempted execve(2)
failed with the error ENOEXEC), these functions will execute the
shell (/bin/sh) with the path of the file as its first argument. (If
this attempt fails, no further searching is done.)
Eu não conheço nenhum sistema que aceite shebangs além de #!
, e certamente duvido que ##!
seja tratado especialmente, mas certamente é possível - o padrão não o impede.