Skip to main content

LFCS Certification Course

Chapter #10: Understanding Terminals, Shells, and Basic Shell Scripting

In this chapter, you’ll learn how Linux terminals and shells work, and how to write your first shell scripts using bash.

Let’s clarify a few concepts first:

  • A shell is a program that accepts commands and gives them to the operating system to be executed.
  • A terminal is a program that allows us as end users to interact with the shell. One example of a terminal is GNOME Terminal, as shown in the below image:
Gnome Terminal

When we first start a shell, it presents a command prompt (also known as the command line), which tells us that the shell is ready to start accepting commands from its standard input device (usually the keyboard).

Shell Options in Linux

Linux provides a range of options for shells, the following being the most common:

bash - Bourne Again SHell

Bash stands for Bourne Again SHell and is the GNU Project's default shell. In addition, it’s the one emphasized on the LPIC-1 exam.

It incorporates useful features from the Korn shell (ksh) and C shell (csh), offering several improvements at the same time.

This is the default shell used by the distributions covered in the LFCS certification, and it is the shell that we will use in this tutorial.

sh - Bourne Shell

The Bourne SHell is the oldest shell and therefore has been the default shell of many UNIX-like operating systems for many years.

Note: On modern systems, /bin/sh is often a symbolic link to /bin/bash or /bin/dash for compatibility and performance.

ksh - Korn Shell

The Korn SHell is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s. It is backward-compatible with the Bourne shell and includes many features of the C shell.

zsh - Z Shell

Zsh is another popular modern shell that combines features from bash, ksh, and tcsh. It’s highly customizable and often used by power users and developers.

Zsh is also the default shell in macOS since Catalina. It supports autosuggestions, improved globbing, and plugin frameworks like Oh My Zsh.

Shell Scripting Basics

A shell script is nothing more and nothing less than a text file turned into an executable program that combines commands that are executed by the shell one after another.

Creating a Script

As mentioned earlier, a shell script is born as a plain text file, and can be created and edited using your preferred text editor.

You may want to consider using vi/vim, which features syntax highlighting for your convenience.

vim myscript.sh

The very first line of a shell script must be as follows (also known as a shebang):

#!/bin/bash

It “tells” the operating system the name of the interpreter that should be used to run the text that follows.

Now it’s time to add our commands. We can clarify the purpose of each command, or the entire script, by adding comments as well.

Note that the shell ignores those lines beginning with a pound sign #.

#!/bin/bash
echo This is Part 10 of the 10-article series about the LFCS certification
echo Today is $(date +%Y-%m-%d)

Once the script has been written and saved, we need to make it executable: