Lua Hello World Example: How To Write and Execute Lua Program on Linux OS

by Ramesh Natarajan on December 25, 2009

Question: I would like to understand the basics of how to write and execute Lua program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World Lua program and execute lua program on Linux or Unix OS.

1. Write a Hello World Lua Program

Create the helloworld program using a Vim editor as shown below.

$ vim helloworld

#!/usr/bin/lua

-- hello world lua program
print ("Hello World!")

Note: Comment in Lua starts with two hyphens “–”.

2. Make sure Lua Interpreter is installed on your system

Make sure Lua interpreter is installed on your system as shown below.

$ whereis lua
lua: /usr/bin/lua5.1 /usr/bin/lua /usr/share/man/man1/lua.1.gz

$ which lua
/usr/bin/lua

Installing Lua Interpreter

If you don’t have lua interpreter, install it as shown below.

$ sudo apt-get install lua5.1

3. Execute the Lua Program

You can either execute using lua helloworld or ./helloworld

$ lua helloworld
Hello World!

( or )
$ chmod u+x helloworld

$ ./helloworld
Hello World!

Note: As lua is an interpreted language, you don’t have the compilation step similar to the C program.

4. Writing and Executing Lua One liner

You can also execute lua from the command line as shown below. This will print Hello World!.

lua -e 'print("Hello World!")'
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Lisp Hello World Example: How To Write and Execute Lisp Program on Linux OS
  2. Ruby Hello World Example: How To Write and Execute Ruby Program on Unix OS
  3. XQuery Hello World Example: How To Write and Execute XQuery Program on Linux OS
  4. Perl Hello World Example: How To Write and Execute Perl Program on Unix OS
  5. Python Hello World Example: How To Write and Execute Python Program on Unix OS
  

Vim 101 Hacks Book

Leave a Comment

Previous post:

Next post: