Aura Pasmin
4 min readNov 28, 2019

What happens when you type ls -l in the shell

This blog post describing step by step what happens when you type ls -l and hit Enter in a shell.

Before giving a statement as such, we will know a little more about the shell since the comment belongs to this

what is the linux shell?

A Unix Shell, or also a shell, is the term used in computer science to refer to a command interpreter, which consists of the traditional user interface of Unix-based operating systems and the like, such as GNU / Linux.

Through the instructions provided by the interpreter, the user can communicate with the nucleus and by extension, execute these orders, as well as tools that allow him to control the operation of the computer.

In the most generic sense of the term, shell means any interpreter that users use to write commands.

how to use the ls -l command?

The ls command with the -l argument and what happens in the process.

First, in your terminal you must enter the ls -l command, of course you must first have the shell installed and it is best to have it updated. The shell reads the command from the getline () function from the Standard input. Then they analyze the command with the arguments and execute them the program.

Then, the shell searches for the alias and checks if the ls command is an alias. If in case the command is not an alias, the shell verifies if the word is incorporated in its library.

NOTE: An Alias as the name implies will help us replace a word or series of words with a shorter and simpler one.

The shell searches for a function with the name ls in the executable files of the system, specifically in the $ PATH variable.

What is PATH?

PATH is an environment variable of the operating systems, it specifies the paths in which the command interpreter must search for the programs to be executed.

This variable must contain all the directories in which you want the interpreter to search for programs, the order being taken into account at the time of the search.

some very important functions that practically form the shell.

getline(): The getline () function is prototyped in the stdio.header file. Here are the three arguments: the address of the position of the first character where the input string will be stored, is the address of the variable that contains the size of the input string, another pointer and the identifier of the input file.

fork(): The call to the Fork system is used to create a new process, which is called the child process, which runs simultaneously with the process that makes the parent process. After creating a new child process, both processes will execute the following instruction after the fork () system call.

It does not take parameters and returns an integer value. Below are different values returned by fork ().

Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created child process.

execve(): Run the program indicated by filename. filename must be either an executable binary, or a shell script (shell script) beginning with a line of the form “# !, interpreter [arg]”. In the second case, the interpreter must be a valid path name for an executable that is not itself a hyphen and will be executed as an interpreter [arg] filename.

wait (): All calls to the wait system are used to wait for status changes in a secondary element of the calling process and obtain information about the element whose status has changed. It is considered a change of state.

exit (): ends the call process immediately. The open file descriptors that belong to the process are closed and the secondary elements of the process are inherited by the process.

The getline () waits for the command entered in the terminal, then the shell creates a fork () and executes the execve (). The Shell searches for the command in $ PATH and executes it, then the shell executes the wait () system call, waiting for state changes in a secondary element of the calling process and does not interfere with the main process if this fails it goes to exit() and then wait().

After executing ls -l, it does the whole process seen above, frees memory, exits and shows the list of the files with their respective information.

EXAMPLE:

write(1, “$ “, 2);
read = getline(&string, &size, stdin);

ls -l

ls -la