LinuxCommandLibrary

lua

Execute Lua programs

TLDR

Start an interactive Lua shell

$ lua
copy

Execute a Lua script
$ lua [path/to/script.lua] [--optional-argument]
copy

Execute a Lua expression
$ lua -e '[print("Hello World")]'
copy

SYNOPSIS

lua [options] [script [args]]
lua [options] -e "chunk" [args]
lua [options] -l file [args]
lua [options] -

PARAMETERS

-e chunk
    Executes the given chunk of Lua code specified as a string.

-l file
    Loads and executes the specified Lua file before processing other arguments. Useful for libraries.

-i
    Enters interactive mode (REPL) after executing script or chunk, if any.

-v
    Prints version information about the Lua interpreter to standard output.

-E
    Ignores environment variables like LUA_PATH and LUA_CPATH when searching for modules.

--
    Stops processing options. All subsequent arguments are passed to the script as literal arguments.

-
    Reads Lua code from standard input. If no script is provided, it enters interactive mode.

script
    The path to the Lua script file to be executed.

args
    Arguments passed to the Lua script, accessible within the script via the global arg table.

DESCRIPTION

The lua command is the official interpreter for the Lua programming language. Lua is a powerful, efficient, and lightweight scripting language designed primarily for embedding in applications. It's known for its small footprint, high performance, and simple C API, making it an excellent choice for a wide range of uses, including game development, web applications, embedded systems, and general scripting. When invoked, the lua command reads and executes Lua source code, either from a specified file, from standard input, or from a string provided on the command line. It provides a flexible and fast environment for running Lua programs.

CAVEATS

The functionality and available modules of the lua command depend on how Lua was compiled and installed on the system. Behavior might differ slightly between major Lua versions (e.g., Lua 5.1 vs. Lua 5.4). Scripts may fail if required modules are not found in the LUA_PATH or LUA_CPATH, or if those variables are ignored with the -E option.

INTERACTIVE MODE

If no script file or -e chunk is provided, or if the -i option is used, the lua command enters an interactive mode (REPL: Read-Eval-Print Loop) where users can type Lua code directly and see the immediate results.

ENVIRONMENT VARIABLES

The lua command respects environment variables such as LUA_PATH and LUA_CPATH, which define the search paths for Lua modules and C modules, respectively. These can be overridden or ignored using the -E option for predictable script execution.

EXIT STATUS

The lua command typically exits with a status of 0 upon successful execution of a script or interactive session. A non-zero exit status usually indicates an error during parsing, compilation, or runtime execution of the Lua code within the interpreter.

HISTORY

Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes at PUC-Rio, Brazil. Initially designed for extending applications, it gained popularity for its embeddability, speed, and small size. Major versions have been released over time, with Lua 5.0 (2003), 5.1 (2006), 5.2 (2012), 5.3 (2015), and 5.4 (2020) introducing new features and improvements while maintaining its core philosophy. Its usage expanded significantly, particularly in game development (e.g., Roblox, World of Warcraft add-ons) and embedded systems, due to its efficiency and ease of integration.

SEE ALSO

luac(1), bash(1), python(1), perl(1)

Copied to clipboard