What happens when you type ls -l in the shell

Cristian Mendoza
5 min readAug 21, 2020

first you have to understand how the shell work and i do not mean olny that shell is a command interpreter. A shell is the most fundamental way that a user can interact with the system, and the shell hides the details of the underlying operating system from the user.

what processes are performed internally to achieve this objective in this post and better understanding of what goes on behind the scenes in your shell. This time we’re going to take a closer look at one of the most common commands any person that handles a linux system can use: the “ls” command. This article is meant to not only tell you what it is but also open your eyes to the processes behind shell.

What is a command ?

A command is an instruction given by a human to tell a computer to do something. Commands are generally issued by typing them in at the command line and then pressing the ENTER key, which passes them to the shell. A shell, also referred to as a command interpreter, is a program that reads commands that are typed on a keyboard and then execute

What is shell behind scene?

Shell is a loop that waits for user input to look for in PATH of Linux. In case you want to see the full Path type “env” and press enter in your shell.

When the shell finds the directory that “ls” exists in, it will create something known as a process. A process as stated before is just simply an instance of a programs that needs execution. The shell itself is a parent process, and the execution of “ls” would be a child process. A process can be created with the system call fork().

The fork() system call will give the child process a PID or process ID number of 0, which helps differentiate it from the parent process who will be given a non-zero value PID. The parent process will want to wait until the child process has executed and completely terminated, and to ensure this, the wait() system call can be used.

The ‘ls’ command is in charge of displaying all the files in the current folder, or more formally speaking it will list the current directory contents.

WHAT IS THE COMMAND ls -l?

To define this term we must split in two this command, ls is a command that allows us to list the files and folders that we have in the directory, -l is a characteristic or behavior specific to our command ls this case will show us information detailed the files and directories, this means What should we consider the commands we can add more specific functionalities, this is known as flags.

The process behind scene

To know if we are typing from the keyboard to our shell we will use the function isatty() that detects if what was written was done std descriptors. you can read all about https://man7.org/linux/man-pages/man3/isatty.3.html

As we already know we must first obtain the input data, for this we use the function getline(), this function allows us read and capture inside a variable the content of what we write on the shell.once we have this information stored we can continue with the next step. you can read all about getline here https://man7.org/linux/man-pages/man3/getline.3.html

Execution process

Once the tokenization is complete, the shell will register each directory in the path and search within each directory for the command. It starts a cycle until the file is found in one of the directories or nothing is found.

After traversing the PATH directories, it uses the statistical function to verify if the program corresponding to the command exists in any of these directories. The stat() function allows me to verify the information of a file through its metadata and to verify if an existing match is found with this data within any directory. If this check is successful, the shell will continue to execute the command with the program found in the corresponding directory.

When we reach the command execution phase, it means that the verification with the statistical function was successful and the corresponding executable file was found. In order to execute this execution, the shell creates a subprocess or child process using a function called fork(), if this child process is created successfully, it is within this process where the execution is carried out through the execve() function running a file or program recognized as executable within the path where this file is located, while this happens, the parent process will remain to wait for execution to finish in the child process.

The wait() function is the one that allows us to indicate the parent process to wait for the execution of the child process to finish, so it needs to capture the information about the state that this execution returns when finished. If this process is successful, it will run the program corresponding to the command entered at startup, in this case, “ls” was passed to execve() and the “-l” option was also passed and thus executed. The complete command “ls -l” has now been run and determined in the terminal, that’s when the child process ends and the shell returns to the parent process so that the entire flow starts again. In case the execution dies, an error message and status will be returned, the child process ends and here also the shell will return to the parent process to start again with the whole flow.

The main process begins again and returns to the beginning of the infinite loop. A command prompt will print and the shell will wait for another command to be typed on the command line and this whole process will start again.

FLOW CHART SHELL EXE

--

--

Cristian Mendoza

Developer | Software engineer in process | Facebook | instagram | Student in Holberton school