Skip to content

Lua Basic Syntax

Lua’s syntax is relatively simple and consistent, making it easy to learn. Let’s start with your first Lua program!

Lua offers an interactive mode, also known as a REPL (Read-Eval-Print Loop). You can type Lua statements one by one and see immediate results. To start interactive mode, open your terminal or command prompt and type lua:

$ lua
Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio
>

You can then type Lua code directly at the > prompt:

> print("Hello from interactive Lua!")

After pressing Enter, you’ll see the output:

Hello from interactive Lua!

To exit interactive mode, you can usually type os.exit() or press Ctrl+D (on Unix-like systems) or Ctrl+Z then Enter (on Windows).

For larger programs, you’ll write your Lua code in a file (typically with a .lua extension) and then tell the Lua interpreter to execute that file.

  1. Create a file named myscript.lua and add the following content:
print("Hello from a Lua script!")
  1. Save the file. Open your terminal or command prompt, navigate to the directory where you saved myscript.lua, and run:
$ lua myscript.lua

The output will be:

Hello from a Lua script!

On Unix-like systems (Linux, macOS), you can make your Lua script executable by adding a “shebang” line at the very beginning of the file and setting execute permissions.

#!/usr/bin/env lua
print("Hello from an executable Lua script!")

The #!/usr/bin/env lua line tells the system to use the lua interpreter found in the user’s environment path. Then, make the script executable:

$ chmod +x myscript.lua

And run it directly:

$ ./myscript.lua

Output:

Hello from an executable Lua script!

A Lua program is composed of various tokens. A token is a sequence of characters that has a collective meaning. Tokens can be keywords, identifiers, constants (literals), operators, or punctuators. For example, the statement:

local message = "Hello, " .. _VERSION .. "!"
io.write(message, "\n")

The individual tokens include: local, message, =, "Hello, ", .., _VERSION, .., "!", io, ., write, (, message, ,, "\n", ).

Comments are explanatory text ignored by the Lua interpreter. They are used to make code more understandable.

  • Single-line comments: Start with -- and extend to the end of the line.
  • Multi-line (block) comments: Start with --[[ and end with ]]. These can span multiple lines and can also be used to temporarily disable blocks of code.
-- This is a single-line comment.
local x = 10 -- This comment is at the end of a line.
--[[
This is a
multi-line comment.
It can span several lines.
]]
--[[
-- You can even nest single-line comments inside, or easily comment out code:
local y = 20
print(y)
--]]
-- Tip: For easily toggling block comments, you can use `--[=[ ... ]=]`
-- To comment out: --[=[
block of code
--]=]
-- To uncomment, just remove one hyphen: -[=[

An identifier (or name) is used to identify variables, functions, or table fields. Identifiers in Lua can be composed of letters (a-z, A-Z), digits (0-9), and underscores (_). They must not start with a digit. Lua is case-sensitive, so myVar and myvar are different identifiers.

Examples of valid identifiers:

name userName _temp count1 MAX_VALUE isValid

Keywords are reserved words that have special meanings in Lua and cannot be used as identifiers. Here are some of Lua’s keywords:

andbreakdoelse
elseifendfalsefor
functionifinlocal
nilnotorrepeat
returnthentrueuntil
while

Whitespace refers to spaces, tabs, and newline characters. Lua uses whitespace to separate tokens but generally ignores extra whitespace. Blank lines are also ignored.

For example, in local age, the space between local and age is necessary to distinguish them as two separate tokens.

local age -- Whitespace is required here

However, in an assignment like fruit = apples + oranges, whitespace around =, + is optional but often used for readability.

fruit=apples+oranges -- Valid, but less readable
fruit = apples + oranges -- More readable with whitespace