GNU/Linux Programmer's Solver - Lua

Hello World program in Lua. Basic structure, comments and string literals
PROGRAMMING LUA HELLO WORLD
#!/usr/bin/lua

-- This is a comment
--[[
This is a comment spread
through multiple lines
]]

print ("Hello World!")
print ([[------------------
This is a character string composed
of multiple lines
-------------------]])

Lua is another relatively popular interpreted programming language. While the syntax is very simple and clear, making it very easy to learn, it presents certain differences compared to the languages we have seen so far.

The first that catches our attention is the way of defining comments using the -- characters, which deviates from what we’ve seen so far, but know that it’s quite common. SQL, for example, uses those characters for comments.

We can also see that there’s no need for an instruction terminator and that we can define multi-line strings by enclosing them between the [[ and ]] characters. As you can see in the code above, this latter can also be used to define multi-line comments.

In Lua, the print function automatically adds a carriage return to the string of characters it prints to the screen, just like puts in C. In the next chapter, we’ll see how to print strings without automatically adding a carriage return, as well as a case where that’s what we want.

To execute our Lua program, we need to give the file permissions and use the appropriate interpreter.

$ lua hello.lua
$ chmod +x hello.lua
$ ./hello.lua

DID YOU KNOW

The Lua language is popular as a scripting language in video games (Roblox, World of Warcraft, DOTA2,..) and in professional tools such as the protocol analyzer Wireshark or the database Redis

Lua was created in 1993 by Roberto Ierusalimchy, Luiz Henrique de Figueiredo, and Waldemar Celes, all of whom were members of the Computer Graphics Technology group at the Pontifical Catholic University of Rio de Janeiro in Brazil. Lua was the predecessor of two previous languages also developed by the same group: SOL (Simple Object Language) and DEL (Data-Entry language). The name Lua (Moon in Portuguese) was chosen as an evolution of the SOL language.

Lua was initially developed in the field of computer graphics, as an alternative to TCL, whose syntax did not excite the authors of this language.

You can install Lua on Debian-based distributions using the command:

apt install lua5.4

Summary

  • Interpreted language
  • Extension: lua
  • Line terminator: carriage return
  • Groups instructions with: Depends on the command
  • Multi-line comments: Using /* ... */
  • Single-line comments: --
  • String delimiter: "string"
  • You can print text with print (Adds carriage return)
  • Supports multi-line strings: [[ string ]]
  • Does not Support HERE-DOCS
  • Entry Point: Beginning of the file

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Go

Hello World program in Go. Basic structure, comments and string literals

Read

PROGRAMMING GO HELLO WORLD

GNU/Linux Programmer's Solver - Rust

Hello World program in Rust. Basic structure, comments and string literals

Read

PROGRAMMING RUST HELLO WORLD

Return to Home Page