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.?
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 ??
Analista de Seguridad Digital | Cyber enthusiast | Full-Stack Software Developer
2 年?? excellent work Maxi and Eze as always!!!