What happens when you type “ls -l” and hit Enter in a Shell ?

Simple Shell was our team project with Aurelie Cedia at the end of the first trimester in Software Engineering Holberton School. The goal of this project is to write a simple UNIX command interpreter. In this article I will describe step by step what happens when you type “ls -l” and hit Enter in a Shell.

  • We were limited by allowed for use functions and system calls, that’s why you will see in some images using, for example, function _strdup (which we recorded) instead of standard strdup.

Read the command line.

The shell runs in an infinite loop. It is always waiting for the next command to be entered by the user. Once a command is entered, the standard input is read and put into a buffer using getline.

Image 1. An infinite loop and getline.

Split line into the tokens.

The buffer gets tokenized. We use function strtok for this. Tokenize means to break up into pieces. Shell breaks up the buffer. After it represents easily readable strings that can be passed to commands.

Image 2. Strtok first step.

Check for alias.

We did not implement aliases in our project, but … Aliases are shortcuts to regular commands. The alias implementation can be done using data structures like:

Check for built-in’s.

In our project we implemented the exit built-in and environment. For check we use our version of function strcmp. Function strcmp takes 2 arguments: the first and the second string to be compared.

Check for binary in PATH (/bin).

We will search for the environment PATH. It contains all the directories where we will look for the executable files. Check if the user’s command exists at the directories of the PATH. If yes, transforms it to the full path, clear for shell.

Execution phase. Fork.

So we found “ls” in the /bin directory. “/bin/ls” is passed into the execution. We will create a child process using a fork.

Software engineering student at Holberton School

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