|
|
Log in / Subscribe / Register

How programs get run

How programs get run

Posted Jan 29, 2015 9:50 UTC (Thu) by drysdale (guest, #95971)
In reply to: How programs get run by wahern
Parent article: How programs get run

Thanks for the clarification & comparisons with other OSes -- I should have made clear that the bundling together of arguments into argv[1] means that multiple interpreter arguments basically won't work.


to post comments

How programs get run

Posted Jan 29, 2015 17:17 UTC (Thu) by vonbrand (guest, #4458) [Link]

Please do update the article with this information. It is definitely one to bookmark.

How programs get run

Posted Jan 29, 2015 21:13 UTC (Thu) by wahern (subscriber, #37304) [Link]

FWIW, Linux and OS X are the only systems I'm aware of that permit recursive shebang execution. Some systems, like Free/Net/OpenBSD, will recursively search for the binary interpreter, but they won't stack the paths of the intervening interpreters. Instead the binary interpreter is only passed the original file path. (And any trailing shebang arguments in the scripts seem to get dropped altogether.)

That's not germane to how Linux executes binaries. But I have a feeling this page might end up near the top of the Google results (as all good LWN articles do) for shebang-related queries, so it's worth putting out there.

Because shells parse scripts line-by-line, if you can come up with a construct that is both valid shell code and valid code in your other language, you can mix interpreters portably. For example, the following is a mixed shell/Lua script which will locate a Lua interpreter. Because both the locations _and_ interpreter names of Lua differ across systems, even across Linux distributions, and even for the same version of Lua, you can't use the #!/usr/bin/env trick to run your Lua scripts and expect it to work even remotely reliably.

#!/bin/sh
_=[[ # variable assignment in shell, beginning of long string in Lua
IFS=:
for D in ${PATH:-$(command -p getconf PATH)}; do
    for F in ${D}/lua*; do
         # check if it's our preferred version
        if ...; then
            exec "${F}" "$0" "$@"
        fi
    done
done
printf "%s: unable to locate Lua interpreter\n" "${0##*/}" >&2
exit 1
]]
-- begin pure Lua code
print(_VERSION)

I recently published a script, runlua, for portable execution of Lua scripts, which is why all of this stuff is still fresh in my mind.


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