Skip to content
BENIBIX Learn

Shell Scripts, Perl and Lua

Level 6 · Scripting languages

Small languages that do boring jobs for you, or live inside other programs. Shell scripts and PowerShell, Perl for text, and Lua for games.

Course contentsLesson 12 of 12

Level 3 · General-purpose and systems languages

  1. Python, Java and C#
  2. C, C++, Rust and Go

Level 4 · Languages for phones

  1. Swift, Kotlin and Dart

Level 5 · Data and functional languages

  1. R, Julia and MATLAB
  2. Haskell, Scala and Elixir

Level 6 · Scripting languages

  1. Shell Scripts, Perl and Lua

After this lesson you can

  • Write a small shell script that runs commands
  • Say how PowerShell differs from a shell script
  • Say where Perl and Lua are used

12 mincovers 4 glossary entries

Worth reading first: Compilers and Interpreters

Which small languages automate jobs

A shell script is a file of commands that the shell runs one after another. The commands are the same ones you would type by hand, such as copy this folder or delete old logs. Put 10 of them in a file, and one command runs all 10. On Linux and macOS, the usual shell is Bash or a close cousin.

Shell scripts are glue. They start programs, move files and check results, and they run on almost every server with nothing to install. They get hard to read past a hundred lines or so. Then a language like Python is the better tool.

PowerShell is Microsoft's shell and script language, and it now runs on Windows, macOS and Linux. Its big difference is what passes between commands. A Bash command hands the next one plain text. A PowerShell command hands over objects with named parts, such as a file's name and size, so there is no text to pick apart.

Perl was made in 1987 for working with text, and it grew famous for finding and changing patterns fast. For years it was the glue of the early web and of system jobs. Its code can be very short and very hard to read. It is less common for new work today, but plenty of old scripts still run.

Lua is small, fast and built to live inside another program. It started in Brazil in 1993. A game engine written in C++ can let designers write levels and rules in Lua without touching the engine. Add-ons for big online games are written in it. Luau, a language built from it, runs a popular game platform.

All four are interpreted, so you edit and run with no build step. Lua, like R, counts lists from 1. A good habit for any script is to stop at the first failed command, rather than carry on and make things worse.

The classic shell mistake is a space in a file name. A script that deletes the old report can split the name in two and remove the wrong files. Wrap every file name in quote marks.

bash
#!/bin/bash
# backup.sh: copy today's work to the backup drive
set -e                       # stop at the first failed command
day=$(date +%F)
cp -r "$HOME/work" "/mnt/backup/work-$day"
echo "Backed up to work-$day"
Four commands you could type by hand, saved in one file. The quote marks keep names with spaces in one piece.
A script saves the commands you keep typing. One line then runs them all, the same way every time.

Worked example

Lucy's nightly clean-up

Lucy deletes old log files on 6 servers by hand each Friday, typing the same 3 commands on each one. It takes about 20 minutes.

She writes a 12 line shell script and schedules it to run every night. The job now takes no time, and it never skips a server.

The words for this lesson4

Check yourself

What does PowerShell pass from one command to the next?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.