|
|
Subscribe / Log in / New account

The case of the supersized shebang

The case of the supersized shebang

Posted Feb 20, 2019 11:19 UTC (Wed) by epa (subscriber, #39769)
In reply to: The case of the supersized shebang by epa
Parent article: The case of the supersized shebang

OK, according to the historical lore linked later in this discussion, it's the shell that is responsible for treating an executable as a shell script if it looks like one. Also Perl's exec() call does it. But the raw system call and C library do not: trying to execl() an executable file which just contains 'echo hello' will fail. It needs the shebang line.


to post comments

The case of the supersized shebang

Posted Feb 20, 2019 11:55 UTC (Wed) by pebolle (guest, #35204) [Link] (3 responses)

Michael Kerrisk's TLPI (p. 575) points to execlp() and execvp() for that behaviour. A quick test showed that execlp() indeed treats such an executable file "as though [it] started with a line containing the string #!/bin/sh".

The case of the supersized shebang

Posted Feb 20, 2019 22:08 UTC (Wed) by foom (subscriber, #14868) [Link] (2 responses)

Some shells *also* handle this themselves.

E.g., with bash, if execve fails with -ENOEXEC, it will reset all the shell state and evaluate the #!-less script file directly in the post-fork bash process, rather than exec'ing anything at all (neither /bin/sh nor /bin/bash!)

The case of the supersized shebang

Posted Feb 20, 2019 22:32 UTC (Wed) by pebolle (guest, #35204) [Link]

I see. So it seems - or, put another way, strace showed me - that this behaviour of /bin/sh is what allows execlp() to do its, well, magic.

The case of the supersized shebang

Posted Feb 22, 2019 8:31 UTC (Fri) by epa (subscriber, #39769) [Link]

Sounds like a handy trick to speed up your shell scripts!


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds