Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

This is part V of a tutorial on how to build a Linux shell. You can read the previous parts of this tutorial from the following links: part I, part II, part III, part IV.

NOTE: You can download the complete source code for Part V from this GitHub repository.

Introduction to Part V

As we’ve seen in the previous parts, a simple command consists of one or more arguments, also known as words. The first word contains the name of the command we want to execute, while the rest of the words contain the command’s arguments.

Before the shell executes a command, it performs word expansion on the command words. Word expansion is the process by which the shell takes a command word, checks it to see if it contains variable names, pathnames, commands, and arithmetic expressions, and substitutes each name/command/expression with its value. The resultant word, which is usually (but not always) longer than the original word, might be broken down into one or more subwords (or fields), in a process known as field splitting. …

Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

This is part IV of a tutorial on how to build a Linux shell. You can read the previous parts of this tutorial from the following links: part I, part II, part III.

NOTE: You can download the complete source code for Part IV from this GitHub repository.

Introduction to Part IV

In this part, we’re going to add symbol tables to our shell. The symbol table is a data structure that is used by compilers and interpreters to store variables as entries in the table. Each entry consists of a key (the variable’s name) and an associated value (the variable’s value). Keys are usually unique, that is, we can’t have two entries that share the same key (i.e., …

Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

This is part III of a tutorial on how to build a Linux shell. You can read the first two parts of this tutorial from these links: part I, part II.

NOTE: You can download the complete source code for Part II & III from this GitHub repository.

Parsing Simple Commands

In the previous part of this tutorial, we implemented our lexical scanner. Now let’s turn our eyes to the parser.

Just to recap, the parser is the part of our Command Line Interpreter that calls the lexical scanner to retrieve tokens, then constructs an Abstract Syntax Tree, or AST, out of these tokens. …

Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

In the first part of this tutorial, we’ve built a simple Linux shell that prints a prompt string, reads input, then echoes the input back to the screen. This isn’t very much impressive now, is it?

In this part, we’ll update our code to give our shell the ability to parse and execute simple commands. Let’s first have a look at what simple commands are.

NOTE: You can download the complete source code for Part II & III of this tutorial from this GitHub repository.

What is a Simple Command?

A simple command consists of a list of words, separated by whitespace characters (space, tab, newline). The first word is the command name, and is mandatory (otherwise, the shell won’t have a command to parse and execute!). The second and subsequent words are optional. …

Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

Since the early days of Unix, the shell has been an important part of the user’s interface with the operating system. The first Unix shell (the Thompson shell) had very limited features, mainly I/O redirection and command pipelines. Later shells expanded on that early shell and added more and more capabilities, which gave us powerful features that include word expansion, history substitution, loops and conditional expressions, among many others.

Why This Tutorial

Over the past 20 years, I’ve been using GNU/Linux as my main operating system. I’ve used many GNU/Linux shells, including but not limited to bash, ksh, and zsh. However, I’ve always been bugged by this question: what makes the shell tick?

About

Mohammed Isam

GNU maintainer, Fedora packager, FSF member, and all-around Linux nerd

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store