Step by step, What happens when you type ls -l in the Shell

Roger Bayron Rendon Munera
6 min readApr 17, 2020

--

Introduction

In this article we are going to explain very briefly what is a shell, how does it work and what happens when you enter a command.

Warning: to fully understand this article you must have basic knowledge about Linux or Unix-based operating systems.

What is a Shell

A shell is a program that allows us to communicate with the operating system through commands given by text. Through a shell you can send orders to your computer to do whatever you want such as running programs, listing files, or connecting to a remote server.

The shell is that famous black screen full of text which allows us to control the entire operating system through commands.

A brief history of the shell

In the late 1960s within the Bell Labs company located in New Jersey, there was a crazy programmer named Ken Tomson who invented the first shell in history. This archaic shell included the basic functions to control the computers of the time and was used in the first versions of the Unix operating system in the first half of the 70s which was also developed within Bell Labs at the same time.

With the passage of time new more sophisticated shells appeared, among those: C shell, Bourne shell, and the famous Bourne again shell (Bash).

How a shell works

The Shell manages and hides the processes and technical details carried out by the core of the operating system (The Kernel). The shell is responsible for delivering the commands that the user types into the operating system.

To manage this delivery, several processes are carried out.

We will briefly explain these processes through an example: what happens when you type the “ls -l” command in the shell?

What happens when you type ls -l in the Shell

We will start by talking about the structure used to write the commands.

All the shells show a prompt on the screen, which is a text that indicates the place where you must write the command, this may vary depending on the shell or the way the user has customized it. We will use as an example this: “$>”

the structure is as follows:

$> <Command> <Options> <Arguments>

In our example “ls” is the command and “-l” is the option, you could also add an argument for example “/var/tmp

The command is the instruction that we will give to the operating system and the arguments along with the options will indicate the way in which it will execute the command. The commands can be built-in shell functions or simply other programs installed in the operating system that the shell will execute.

In this case “ls” is a program installed on all unix-based operating systems. The ls function lists all the files and directories in the current directory, unless you specify the specific path as a argument.

For more information about ls: http://man7.org/linux/man-pages/man1/ls.1.html

The moment you hit the enter key after you enter the command the the spicy part starts…

We are going to list step by step what the shell does from the moment you press the enter key.

The process

When the shell is executed enters in an infinite loop in which in each iteration it pauses and waits for you to enter a command.

Once the command is entered, the shell will tokenize all the command text which means it will store each word separately; the command, options and arguments will each be stored independently, making it easy for the shell to process each word separately.

In a nutshell the command entered, in this case “ls -l” will be divided, and both “ls” and “-l” will be stored independently.

The shell will parse the first word, in this case “ls”, and will check if it is a built-in function or not; if it is, it will execute it, otherwise it will check if it is a program installed in the operating system; In this case “ls” is a program.

Since ls has been ruled out as a built-in shell function, it will proceed to check if “ls” is a program installed in the operating system. For this task, the shell will use an environment variable called PATH.

Environment variables store useful information that programs installed on the system use to fulfill their functions.

The PATH environment variable contains all the paths to the executable files of the programs installed in the operating system.

The shell will use paths registered in the environment variable PATH, to search for any executable file whose name is equal to the command entered.

In case of finding an executable file that matches the entered command, a series of system calls will be made to execute the program; otherwise, an error message will be displayed on the screen.

A system call is a mechanism by which a program requests permission from the operating system to make use of a special system resource service such as: executing files or creating processes.

In order to execute the program, the shell will use two system calls which will allow the shell to carry out the following actions:

1- The creation of a thread which is known as a child process, this action is known as forking.

2- The execution of the command or rather the program which is being called.

Creating a child process is mandatory to prevent the shell from stopping after executing the program, the reason for this is that once the new program is executed, it will take over the process from where it was run, killing the shell in case it runs in the same process.

A child process will be created where finally the command or program will be executed in parallel to the shell. The behavior of the execution will depend on the options and arguments entered next to the command.

In our case, ls which is located in the / bin / path, will be executed, and as a result all the files and directories that are in the directory from where the command was executed will be shown, and also thanks to the option “- l “entered next to the command, all the attributes of the files will be displayed.

After all this the shell will come back waiting for a new command to be entered.

Written by:

Daniel Perez — (https://www.linkedin.com/in/daniel-perez-b9a357111/)

Roger Rendon — (https://medium.com/@1584)

--

--

Roger Bayron Rendon Munera
0 Followers

Systems Engineer of the Maria Cano University Foundation, CISCO Routing and Switching Studies, currently Full Stack Developer Training in Holberton