What happens when you type ls -l in the shell
Let’s start explaining, What is “the Shell”?
A shell is a program whose primary purpose is to read commands and run other programs. As simple as that!
What are the steps the shell processes a command?
First of all, the shell displays a prompt ($) and waits for the user to type a command; that is called PS1 (Prompt String 1). The shell program reads the user input and stores it as a string into a buffer using a function called getline().
For example, when you type ‘ls’ in the shell. The computer understands something like this:
Let’s look at an example, typing ‘ls’ in the shell and hit enter:
Hold on… But, what is ‘ls’?
In the system Linux/Unix, the ‘ls’ is a command that lists content of directories and files. To memorize ‘ls’ easily think it’s short for the word “list”.
In the above example, the blue files are directories; those are special type of files that contains a list of objects (files, links, directories…). The white name, is just a file and the green is an executable, object code (we will not delve into this in this blog).
…And, What about ‘ls -l’?
The ‘-l’ means “use a long listing format”. So, when you type this command in your shell program and hit enter you will get a long detailed list:
That’s interesting… But let’s continue with the command steps.
The second step is when the shell checks for “Aliases and Special Characters”
What does it mean? When you type in the command line and hit enter, your shell program checks the command you have typed for any aliases associated.
If the shell finds an alias, it expands that alias before it executes. After the shell program checks all aliases, it is going to look for special characters such as: *, “, ‘, \, *, &, # and then executes them.
If the shell does not find the command, it goes through and checks: ‘built-in’ commands.
What is a built-in?
The ‘built-in’ is a command that is part of the shell itself, that means it doesn’t need an external program to run the command. For example: echo, cd (change directory).
If the ‘ls’ is not a built-in command, then the shell will search in the PATH (environment variable), a list of directories to see if the file or program exists. An environment variable is a dynamic value that the operating system and other software can use to determine information specific to your computer.
Let’s look at an example of ‘environment variables list’ typing ‘env’ in the command line:
The shell will use a syscall (way in which a computer program requests a service from the kernel of the operating system) called access() to look for ‘ls’ in every directory (tokens separated by a colon ‘:’) in the PATH. PATH=/usr/local/sbin/ls, /usr/local/bin/ls…
If ‘ls’ is not an existing file, the shell will throw an error message; but if the file is found, the shell will open and run the file ‘ls’ using the syscall fork().
Wait, wait… And what is fork?
Fork is a system call… what is a system call? is the programmatic way in which a computer program requests a service.
The fork system call is used to create a new processes. The newly created process is the child process. The process which calls fork and creates a new process is the parent process. The child process then calls execve() syscall to execute the file. But before the fork ends, the father must wait for the child to finish so it can finish too, for that it uses the wait() syscall, and then executes with the execve().
I hope this blog was useful for you… See you next time :)