Lua - Print Hello World



This tutorial will teach you how to write a simple Hello World program using Lua Programming language. This program will make use of Lua built-in print() function to print the string.

Hello World Program in Lua

Printing "Hello World" is the first program in Lua. This program will not take any user input, it will just print text on the output screen. It is used to test if the software needed to compile and run the program has been installed correctly.

Steps

The following are the steps to write a Lua program to print Hello World –

  • Step 1: Install Lua. Make sure that Lua is installed on your system or not. If Lua is not installed, then install it from here: https://github.com/rjpcomputing/luaforwindows/releases
  • Step 2: Choose Text Editor or IDE to write the code.
  • Step 3: Open Text Editor or IDE, create a new file, and write the code to print Hello World.
  • Step 4: Save the file with a file name and extension ".lua".
  • Step 5: Compile/Run the program.

Example - Lua Program to Print Hello World

Consider the below code that will print "Hello World" or any message that you will write inside the print() method on the screen.

main.lua

# Lua code to print "Hello World"
print ("Hello World!")

In the above code, we wrote two lines. The first line is the Lua comment that will be ignored by the Lua Compiler, and the second line is the print() statement that will print the given message ("Hello World!") on the output screen.

Output

Hello World!

Different Ways to Write and Execute Hello World Program

Using Lua Interpreter Command Prompt Mode

It is very easy to display the Hello World message using the Lua interpreter. Launch the Lua interpreter installed and issue the print statement from the Lua prompt as follows −

Example - Using Command Line Interpreter

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print("Hello World!")
Hello World!
>

Similarly, Hello World message is printed on Linux System.

Example - Using Linux terminal

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print("Hello World!")
Hello World!
>

Using Lua Interpreter Script Mode

Lua interpreter also works in scripted mode. Open any text editor, enter the following text and save as hello.lua

hello.lua

print ("Hello World!")

For Windows OS, open the command prompt terminal (CMD) and run the program as shown below −

C:\>lua hello.lua

This will display the following output

Hello World!

To run the program from Linux terminal

$ lua hello.lua

This will display the following output

Hello World!

Using Shebang #! in Linux Scripts

In Linux, you can convert a Lua program into a self executable script. The first statement in the code should be a shebang #!. It must contain the path to Lua executable. In Linux, Lua is installed in /usr/bin directory, and the name of the executable is lua. Hence, we add this statement to hello.lua file

hello.lua

#!/usr/bin/lua

print ("Hello World!")

You also need to give the file executable permission by using the chmod +x command

$ chmod +x hello.lua

Then, you can run the program with following command line −

$ ./hello.lua

This will display the following output

Hello World!

Thus, we can write and run Hello World program in Lua using the interpreter mode and script mode.

FAQs

1. Why the first program is called Hello World?

It is just a simple program to test the basic syntax and compiler/interpreter configuration of Lua programming language.

2. Installation of Lua is required to run Hello World program?

Yes. Lua installation is required to run Hello World program.

3. How do I run a Lua program without installing it?

TutorialsPoint developed an online environment where you can run your codes. You can use the Lua online compiler to run your Lua programs.

4. First Program Vs Hello World Program in Lua?

There is no difference. The first program of Lua is generally known as the Hello World program.

Advertisements