7 – Lua Standalone

    The options are:

    • -e *stat*: executes string stat;
    • -l *mod*: “requires” mod;
    • -i: enters interactive mode after running script;
    • -E: ignores environment variables;
    • --: stops handling options;
    • -: executes stdin as a file and stops handling options.

    After handling its options, lua runs the given script, passing to it the given args as string arguments. When called without arguments, lua behaves as lua -v -i when the standard input (stdin) is a terminal, and as lua - otherwise.

    When called without option -E, the interpreter checks for an environment variable LUA_INIT_5_2 (or if it is not defined) before running any argument. If the variable content has the format @*filename*, then lua executes the file. Otherwise, lua executes the string itself.

    When called with option -E, besides ignoring LUA_INIT, Lua also ignores the values of LUA_PATH and LUA_CPATH, setting the values of and package.cpath with the default paths defined in luaconf.h.

      will first set a to 1, then print the value of a, and finally run the file script.lua with no arguments. (Here is the shell prompt. Your prompt may be different.)

      Before starting to run the script, lua collects all arguments in the command line in a global table called arg. The script name is stored at index 0, the first argument after the script name goes to index 1, and so on. Any arguments before the script name (that is, the interpreter name plus the options) go to negative indices. For instance, in the call

      the interpreter first runs the file a.lua, then creates a table

      1. arg = { [-2] = "lua", [-1] = "-la",
      2. [0] = "b.lua",
      3. [1] = "t1", [2] = "t2" }

      and finally runs the file b.lua. The script is called with arg[1], arg[2], … as arguments; it can also access these arguments with the vararg expression ‘...‘.

      In case of unprotected errors in the script, the interpreter reports the error to the standard error stream. If the error object is a string, the interpreter adds a stack traceback to it. Otherwise, if the error object has a metamethod __tostring, the interpreter calls this metamethod to produce the final message. Finally, if the error object is nil, the interpreter does not report the error.

      When finishing normally, the interpreter closes its main Lua state (see ). The script can avoid this step by calling os.exit to terminate.

      To allow the use of Lua as a script interpreter in Unix systems, the standalone interpreter skips the first line of a chunk if it starts with #. Therefore, Lua scripts can be made into executable programs by using chmod +x and the #! form, as in

      (Of course, the location of the Lua interpreter may be different in your machine. If lua is in your PATH, then