100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Your First Lua Script

Write, run, and structure a small real Lua script, covering command-line execution, comments, print, and basic input.

FoundationsBeginner8 min readJul 10, 2026
Analogies

Your First Lua Script

The traditional first Lua program is a single line: print("Hello, World!"). Save it in a text file with a .lua extension — say, hello.lua — and you have a complete, runnable Lua script; there's no main function requirement, no class wrapper, and no explicit entry point beyond the top of the file, which executes top to bottom as Lua reads it. This makes Lua an unusually approachable language for a first script: the file's top level is itself the program's execution path, and functions or tables you define simply add structure as the script grows.

🏏

Cricket analogy: Lua needing no boilerplate before print("Hello, World!") runs is like a street cricket match starting the moment someone chalks a wicket on a wall — no toss ceremony, no official scorer, just bat and ball ready to go immediately.

Running Lua Scripts from the Command Line

Once hello.lua exists, run it with lua hello.lua from the terminal, and Lua interprets the file directly — there's no separate compile step you have to invoke manually, though Lua does compile to bytecode internally before execution. On Unix-like systems, you can also make a script directly executable by adding a shebang line #!/usr/bin/env lua as the very first line and running chmod +x hello.lua, after which ./hello.lua runs it like any other command-line tool. Command-line arguments passed after the script name are collected into a global table called arg, so lua greet.lua Aria makes arg[1] equal to the string "Aria" inside the script.

🏏

Cricket analogy: Running lua hello.lua with no separate compile step is like a batsman walking straight to the crease and facing the first ball, with net practice happening invisibly in the background rather than as a separate visible stage.

Comments, print(), and Basic Input

Single-line comments start with --, running to the end of the line; block comments use --[[ to open and ]] to close, spanning multiple lines. The print() function accepts any number of arguments, separated by tabs in the output, and automatically calls tostring() on each one, so print(1, "two", true) prints 1 two true without you needing to manually convert types. For reading input from the user, io.read() reads a line from standard input as a string by default, or you can pass "n" to read and parse a number directly, as in local age = io.read("n").

🏏

Cricket analogy: The -- comment syntax marking a line as non-executable is like a commentator's aside during a broadcast — heard by the audience for context, but never counted as part of the actual match action.

Structuring a Simple Script

As a script grows past a single print line, it's idiomatic to wrap repeated behavior in functions and use local variables to hold intermediate state, even in a small script. A typical first 'real' script reads some input, does a small amount of processing — like a conditional or a loop — and prints a result, tying together the exact features covered in this course's early topics: variables, types, operators, and the print/io.read functions. Keeping the whole thing in one file is completely normal at this stage; Lua doesn't require splitting code into modules until a project actually grows large enough to benefit from it.

🏏

Cricket analogy: Wrapping repeated logic in a function partway through a small script is like a club team finally assigning a specific wicketkeeper only once the games start getting competitive — you don't need the formal role for a casual backyard knockabout.

lua
#!/usr/bin/env lua
-- greet.lua : a small first script that ties variables, types,
-- operators, comments, and input/output together

local name = arg[1] or "friend"   -- read from command-line, default if missing

print("Hello, " .. name .. "!")     -- string concatenation

io.write("How many cups of coffee have you had today? ")
local cups = io.read("n") or 0       -- read a number from stdin

--[[ Give a short, simple response based on the number of cups.
     This is a multi-line block comment. ]]
if cups >= 4 then
  print("That's a lot of coffee, " .. name .. "! Maybe some water too.")
elseif cups > 0 then
  print(name .. ", you've had " .. cups .. " cup(s) today.")
else
  print("No coffee yet, " .. name .. "? Bold choice.")
end

Run this script two ways to see both input methods in action: lua greet.lua Aria supplies the name via a command-line argument (arg[1]), while typing a number when prompted demonstrates io.read("n") reading from standard input.

  • The simplest complete Lua program is a single line: print("Hello, World!") saved as a .lua file.
  • Lua scripts have no required main function or class wrapper — execution starts at the top of the file.
  • Run a script with lua filename.lua, or make it directly executable with a shebang line and chmod +x.
  • Command-line arguments are collected into a global table called arg (arg[1], arg[2], ...).
  • -- starts a single-line comment; --[[ ... ]] opens and closes a multi-line block comment.
  • print() accepts multiple arguments, auto-converts them with tostring(), and separates them with tabs.
  • io.read() reads a line as a string by default, or io.read("n") reads and parses a number.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#LuaStudyNotes#YourFirstLuaScript#Lua#Script#Running#Scripts#StudyNotes#SkillVeris#ExamPrep