What happens when you type `ls -l *.c` in the shell?
Gastón Larroque
Commercial Assistant at WillInn | Back-end Developer | DevOps Student | Scrum | Python | FastAPI | Flask | AWS | Terraform | Docker | JavaScript | C | Linux | Office | Holberton School Alumni
As an introduction to this blog, it’s important to explain, in a few words, what is a terminal and which is the? function. When talking about a shell, we reference a command-line interpreter? that basically translates the user’s interactions with the system. Historically, in the programmation’s world, the first UNIX’s shell was developed by Ken Thompson in the 70’s, and was the base for the later UNIX’s shells.
Internally, a shell is a not-ending process always in execution, that only finishes when the user indicates by a command that stops the shell. The user can always see on screen that the shell prints a “prompt” waiting for an user's order. The prompt can vary, generally is “$” or “#” or any other symbol. When the user presses ENTER, the shell will try to execute the user’s input; execution finished, will print again the prompt on the screen.
We can ask, why doesn't the shell stop when it finishes interpreting a line? Because, internally, a shell uses various syscalls (abbreviation of system call). To keep the shell in execution and at the same time execute a command, it uses the syscall fork, which creates a new process from the first process and runs both in parallel (the first process is called parent process, and the new process its child process). Parent and child processes are different, the system assigns different PID for each one.
Now, we must separate what happens in the parent process and in the child process. In the last one, the system will find to execute the input’s user using another syscall execve, ?which is the one that executes the command with its arguments (if the users write any); when the execve finishes, the child process finishes.
But, at the same moment the parent process is still in execution; to avoid strange behavior, it’s important that the parent process finishes after the child process finishes. This is possible by the syscall wait which forces the parent process to wait for the child process, in order to continue running.
The follow diagram helps us to understand the above idea:
领英推荐
Another shell’s propriety is that it’s not necessary to input a full path’s command to execute it. For example, when a user input “ls” on the shell, prints on the screen the content of the current directory automatically; this occurs because the shell works with “aliasses”.
Following the previous example with the “ls” command, arguments that will be interpreted by the system. For example, if the user input “ls -l *.c”, the shell will find “ls” command; next, will interpret the “-l” argument in the sense that list the current directory in long format, i. e., which will give the user detailed information of each file or directory of the current directory; at last, the shell will interpreter the “*.c” argument that will be responsible to list only the files that finish with “.c”. That means the symbol “*” indicates to the shell, in this case, the user only look for the files that ends with what is written to the right of “*”.
Finally, the command-lines that the shell is able to interpret are pre-defined on the system, therefore any try to execute a invalid command will appear an error message on the screen, showing to the user that the process wasn’t successful because an invalid command was requested.