Lua Hashbang - Executable Lua Scripts

So you have a lua file, and you want to be able to run it from the command line like ./your_script.lua. In languages like python or ruby you would accomplish this by adding #!/usr/bin/env ruby or #!/usr/bin/env python as the first line of the file. The #! magic pattern, known as a shebang (sheh-bang) or hashbang, tells linux to use a specific command to execute the file. However, the python and ruby examples only work because # starts a comment in those languages, so the interpreter ignores it. In lua, our comments start with --, not #, so we need to get clever.

The following solution is modified from code by William Ahern on the lua-l mailing list. This will search $PATH for lua, lua5*, and luajit*, executing the script with first one it finds:

for dir in $PATH; do
    for lua in "$dir"/lua "$dir"/lua5* "$dir"/luajit*; do
        if [ -x "$lua" ]; then
            exec "$lua" "$0" "$@"
printf '%s: no lua found\n' "$0" >&2
exit 1

-- Now we're running lua code
print("lua code here!")

So there you have it. Add those 14 lines at the top of your lua script, mark it as executable with chmod +x your_script.lua, and you’re done! You can now run ./your_script.lua as a command.

Wow, how does that even work?

First, notice our hashbang is #!/bin/sh, so linux is actually going to run this file as if it’s a shell script. When running as a shell script, the code searches for an available lua interpreter on the system, and then re-executes the file as a lua script. Our code therefore means two things in two different programming languages!

Let’s break this down.

When running as /bin/sh:

Then, when running as lua code:

There’s one other nuance of shell scripts that makes this work. Most scripting languages these days will try to parse an entire file before running any of it. A language like that won’t run your code if there’s syntax errors anywhere in the file. So, given most lua probably isn’t going to be valid shell script code, why does this trick work?

Well, sh and derivatives like bash work differently. They parse the file line by line as they execute, instead of parsing the whole thing at once. That means they don’t care if you have a syntax error half way down the file; if they never get to that line of code then the error doesn’t exist! Tools like makeself even use this behavior to create self-extracting tar files, by adding a small shell script to the beginning of the tar file that extracts the rest of it.

And that’s it! That’s all the magic. Now go write some lua scripts!