What happens when you type `ls -l *.c`? in the shell?
Written by Ezequiel Silva & Maximiliano Alonso for Holberton School project.

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

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

If we had had to answer this question ten days ago, without hesitation we would answer “all the files in the current directory which ends with the .c extension will be listed in long format”.

But now we are capable of explaining in detail what happens inside the shell when we type that command line so that’s what we are going to do in this article.

Before we start, we would like to share with you some short definitions that we consider are going to help you to have a better understanding of the following article.

PS1: is a primary prompt variable. Currently it holds \\u@\\h:\\w\\$ special bash characters. This is the default structure of the bash prompt on many Linux systems and is displayed every time you log in using a terminal.

Alias: an alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command.

Built-in: shell built-in is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute.

Now that we have already defined some important concepts, we are in condition to start with our analyze.

The first thing to happen when we open a shell is the printing of the PS1, it indicates that the shell is waiting the input for the user. When the user types the command line the shell read of that input, once the input was read, for example ls -l *.c, the expansion of “*” comes. The wildcard “*” replaces the “*.c” with a list of files in the current directory ending with the extension “.c.”

Ok now the shell has the new complete command line (including the files names), so the next step is parsing that command line into smaller tokens using a delimitator as separator, in this case the delimitator is going to be the whitespace.

Those smaller tokens which where parsed are stored one by one. The first token, ls in this case, is going to be checked multiple times.

1st check - Alias: if it is an alias, the shell replaces the token with its value and execute it.

2nd check - Built-in: if it is a built-in the sell executes that built in.

3rd check - Command: The shell tries to find the command in the PATH, if there is a match the shell calls the program ls with the rest of the arguments as parameters.

When the shell matches the first token with an executable file, it forks the program to create a new process where the program is going to be executed. That new process is called child and it is a duplicate of the calling process the calling process is called parent process. The child process is an exact duplicate of the parent process except for some points, for example, one of them is that the child has its own unique process ID, and this PID does not match the ID of any existing process group or session. It is very helpful to identify them correctly.

When the child process is created the both process parent and child run in separate memory spaces. The child process is the one who calls the execution of the ls file while the parent process waits for the execution

When the execution is called it causes the program that is currently calling the process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

Finally, when the program is done, the PS1 prompt is printed again waiting for a new command to be entered by user.?

No hay texto alternativo para esta imagen


Mouna Ben Ali

React JS, Frontend Web Developer

2 年

Well done ??. If I may suggest one tip: It would help if you did add some code snippets from screenshots from your terminal. I like the flowchart down below very nice ??

Danielle Serafim

Analista de Seguridad Digital | Cyber enthusiast | Full-Stack Software Developer

2 年

?? excellent work Maxi and Eze as always!!!

要查看或添加评论,请登录

Maximiliano Alonso的更多文章

  • PIN! - Plan It Now

    PIN! - Plan It Now

    When we had to choose a project to develop as MVP, we wanted to create something that would be useful to satisfy a need…

    1 条评论
  • What happens when you type google.com in your browser and press enter?

    What happens when you type google.com in your browser and press enter?

    Introduction Have you ever wondered what happens when you type google.com in your browser and press enter? In this…

  • IoT - Internet of Things

    IoT - Internet of Things

    What is IoT? IoT stands for Internet of things, but what do we refer when we say the internet of the things? This…

  • What is recursion?

    What is recursion?

    Introduction I clearly remember the first time I saw the recursion concept in Holberton. We were learning how to…

  • Mutable, Immutable... everything is object!

    Mutable, Immutable... everything is object!

    Introduction In my previous article I talked about object-oriented programming and what classes and instances are. But…

  • Class and instance attributes

    Class and instance attributes

    Before I start talking about classes and instances, I would like to share a brief introduction about what…

    1 条评论
  • Dynamic libraries vs Static libraries

    Dynamic libraries vs Static libraries

    In my previous article, I talked about what a library is in C, why we use them, and specifically how to create and how…

  • Two's complement and negative numbers

    Two's complement and negative numbers

    Introduction In mathematics, when we need to write the sign of a number we put the sign character at the beginning of…

  • C - Static libraries

    C - Static libraries

    In computer science, a library is a collection of non-volatile resources used by computer programs, often for software…

  • Compilation process in c - What happens when you type gcc main.c?

    Compilation process in c - What happens when you type gcc main.c?

    In this blog I’m going to explain you the compilation process in C, how many steps it has and how it works. But before…

    1 条评论

社区洞察

其他会员也浏览了