Every other lesson assumes you can run a snippet and see what it prints.
Lua ships as a tiny C program. Install it from your package manager (lua5.4) or build it from source in seconds. Run lua with no arguments for an interactive prompt, or lua script.lua to run a file; extra command-line arguments arrive in the global table arg.
print writes its arguments separated by tabs and ends the line. io.write writes without separators or a newline, which is handy when you want full control over output.
- A comment starts with
--and runs to the end of the line. - A block comment is
--[[ ... ]]. - Semicolons between statements are optional and rarely used.
- Lua is case sensitive:
printandPrintare different names.
In many places you will not run Lua directly: it is embedded in a host program (Neovim, Redis, a game engine) that loads your script and exposes its own API. The language is the same everywhere, but the available libraries depend on the host. Standalone Lua gives you the standard library: string, table, math, io, os, coroutine and utf8.
LuaJIT and Roblox's Luau are popular dialects based on Lua 5.1; most of what you learn here transfers, but 5.4 features like integer division and <close> variables may be missing there.
Example
-- hello.lua: run with `lua hello.lua a b`
print("Hello, Lua!") --> Hello, Lua!
print(1, "two", true) --> 1 two true
io.write("no newline", "\n") --> no newline
print(_VERSION) --> Lua 5.4
--[[ A block comment
can span lines ]]
print(#arg) --> number of script arguments (2 above)Remember
lua file.luaruns a script;luaalone opens a REPL--starts a comment,--[[ ]]a block comment- The host program decides which libraries are available
Watch out for
- Assuming every host has
ioandos: sandboxed embeddings often remove them.