← Back to Lua Course | Chapter 1: Introduction | Lesson 6 of 7

Running scripts and the REPL

Run a file with the lua command, or type lines one by one in the interactive prompt.

Running scripts and the REPL

Running lua with no arguments starts the interactive REPL, where each line is executed immediately. Running lua script.lua executes a file, and extra command-line words become the global table arg. The option -e runs a snippet directly, and -i drops into the REPL after a script.

Note: In the REPL of Lua 5.3 you can type =expression to print its value.

Example: Running scripts and the REPL

bash
$ lua
Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> x = 10
> =x * 2
20
> os.exit()
$ lua hello.lua alpha beta
$ lua -e "print('hi')"
hi

⚠️ Run this in your own terminal or Node.js environment.

Common Mistakes
  1. Typing lua without a file and expecting nothing to happen
  2. Forgetting the .lua extension in the file name
  3. Expecting arg to exist inside the REPL
Chapter Summary
  • lua alone opens the REPL
  • lua file.lua runs a script
  • arg holds command-line arguments
  • lua -e runs a string of code
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.