What happens when you type ls -l in the shell

Andres Ariza
6 min readApr 19, 2020

This tutorial is made to make a little bit more clear the process of what happen when you type ls -l in the shell. This is something that involves a lot of steps until the list in long format is displayed in the screen. There is a review of each step.

Do you know about the Shell?

The shell its a program that read an order from the user input line and does what the order is programmed to do. This orders are called commands.

This is a prompt

Everything starts in the Terminal

The terminal its the program that can run the shell, we usually open the terminal first before running the shell command. When we open the shell it displays something known as prompt. The prompt is where one types commands so the Shell will be able to read the command and do the stuff.

echo command to the first default prompt

So the shell know how to read?

We could say that yes, it knows what we are saying exactly but the programs are made to return logical structures of the request we made, if we write to the shell something there is a program called getline. Getline can store the line we just type in the prompt, this way we can manipulate each part of the string of letters we send to the prompt.

Ok now we have the string whats next?

Now we have all the data in a box, and we need to separate this box in a structure more readable for the machine. We need to split the string of words into words. I know it sounds weird, but we are used to know when a word end using an space, but the space for the computer its just another symbol, he will not recognize the diference between words if we dont tell him, “hey we use spaces to separate words”. The next step we are gonna use strtok. Called also String tokenizer. This function stores each character of the string until it finds an space. or whatever delimiter we want to use ( .-,;:_?)() etc…

Its important to know that we need to allocate in memory each string we are tokenizing with malloc in order to store the data propperly. At the same time it stores the pointer of this new token in an array of pointers, wich makes a list of all the words we need to continue. This process of divide the words by a delimiter argument its also known as parsing.

We have parse, tokenize and store the words, what we should do now

Now we have to choose the destination of how im gonna use the data we already have. There is a hierarchy and the shell will check from the first to the last of this hierarchy if there is a match with the first token we have in the array. It means its gonna check for the alias(wich is a replacement of another word), then it has to look on the builtins commands and environment of the program. If there still not a match, its necesary to check in the local directory if there is a ./ (executable file) and last step is checking the PATH folders where all the executables are stored. If there is no match in all of this cases the shell should send an error message.

PATH value in environment list variables

And we finally find a match, what we write its a command!

Allright we are now in the same page, and we should make it work. Now we know that the exe file exist, we have to use the execve function, and before that its necesary to creeate another process besides the one we are working right now, so we call another two funcitons, called fork and wait. Wait will be usefull because its gonna prevent some errors that fork could generate. These functions are also called Systemcalls and they differ from the others because they made the request directly to the kernel(we can assume its the head of the program, like the boss).

Fork behaviour

Because its own nature. Fork makes an exact copy of the current process. That copy its known as the child of our main process(parent process). The child will execute our program and the parent will wait to the execution. If the parent dont wait for the child bad things are gonna happen and they are related with zombies and demons so we dont wanna go that far with them. This process have their own ID and Wait will need to work with the PID of the child so it will know when he returns, otherwise it can wait for another child without even notice! and that are also bad news for every single parent in the world.

The parent wait for the child to do the task thats correct

So our child process return with the proceess executeed as we told him to. But in order to make this task, it should get over the hierarchy and check for each location before PATH. The first are Aliases and they are like nicknames for the functions so we can calle them as we want and the Aliases will redirect the value of the variable to the program. After there are the buildins and the only diference is they use pointer to functions.

Example of ls -l execution in a Thompson shell (sh)

Then it looks in the env functions, it stands for environment and they are variables that are inherited from the parent process and they are put by defaut in the program, There is a variable inside env called PATH, it works in our favor because it tell us the directories wich are stored all the executable filese so in the last option it will end looking in the current directory for some the executables or in the PATH, the PATH environment variable has a singularity, its separated by ‘:’ and they indicate to the program where the PATH ends and the next starts. So the search its not gonna be in one single folder but in many.

Example of ls -l execution in a normal shell

Thats cool but what if something fails

The error message will be printed and will indicate the respective message error using perror as the command that displays the respective error. The exit of the program also can be handled in a normal sh with the ctrl+c or cntrl+d.

And probably the most important challenge of this road its, let evertything clean and organized!, Valgrind its a constant ally to this situations, and if you forget to check the free of a malloc, your program will probably impact the performance of your computer. And last but not least. Checking for erros to compilations and looking for a beauty and organized written code its always important if you want to make changes in the near or distant future. or if you are gonna share your magic with others.

THANKS FOR READING c:

AUTHORS

Fredy Acuña

Andres Ariza

--

--