What happens when you type`ls -l *.c` in the shell ?

Joudirim
4 min readApr 14, 2021

Hello, this blog was written for Holberton school as a task of the simple shell making project as software engineering students , after 3 months of hard work and C programming learning.

In the blog we are going to explain what actually happens when typing ls -l *.c in shell and to understand how it really works.

Before moving forward with explanations we need to answer some basic questions;

What is a Shell..?

The Shell is a command line interpreter, it takes user commands and converts it into a language that the computer can understand.

Whenever we enter any command in the text format, it gets translated to computer-readable form and the processed output which is in binary format gets converted to human-readable form and gets displayed on the screen in the form of texts.

Shell performs all these translations.

As an interpreter, the shell operates in a simple loop:

It accepts a command, interprets the command, executes the command, and then waits for another command. The shell displays a prompt($), to notify you that it is ready to accept your command.

What is a system call..?

The system call is the fundamental interface between an application and the Linux kernel.

let’s man ls :

let’s try it on the terminal:

So the ls command is used to view the contents of a directory.

Let’s try ls -l:

And ls -l is used to list the content of the directory in a long format.

Then let’s see the output of ls -l *.c:

we can see that *.c is used to put on all files that ends with “.c” listed in long format.

The long format contains informations about permissions, number of hard links, owner, group, size, last-modified date and filename.

How it works..?

First of all splitting the input and turns it into tokens with strtok():

Then the first argument is held for checking if it is a built-in or a command then the shell parses each token alone.

In case of built-in such as (cd, echo, history, exit..) the shell automatically execute it.

what we mean by built-ins?

Built-ins are commands or functions called from the shell.

These functions are executed directly within the shell in the present process instance and are faster than external programs

If the program determines the user input is not a built-in, by comparing the command with an array of the built-in names, then it moves to the next process, which is looking for the PATH.

It copies the environment, which is an “array of strings”, One of this strings is the PATH. Which as such is a line starting with “PATH=” followed by the addresses of all the directories where the executable files are located separated by colons.

In our case for ls the shell will start looking for the program file named ls, in the possible addresses stores in the PATH.

Now after finding ls, shell will execute it using the following system calls:

fork creates a child process. When this function is called, the operating system duplicates the process currently running, both start to run concurrently. The original process is called the “parent”, and the new process is the “child”.

execve executes the program. when calling execve, the operating system stops the original process, and starts that one in its place.

wait stops the parent process until the child finish it’s process.

when the ls is executed the system call that provide the execution take all the other argument from the input in our case the -l argument into the other executable program (ls).

Finally the prompt is printed waiting for a new command to be entered.

Thanks for reading our blog and feel free to check our simple-shell project repository.

Authors:

Marwa Alaya

Rim Joudi

--

--