In this article, I will going in depth with the answer for this question. This is a command written on a command-line interpreter. A command-line interpreter is a program opened in a text editor made to take in commands from the user and executes them on the prompt. After successful execution, it outputs the action of the command and then a new prompt afterwards. On failure, it will produce a error message and a new prompt afterwards. Check out the caption below the photo for a break down of what is happening in this screenshot.
The output is my files and directories listed in long, vertical format. That also includes symbolic links, identified by a file name pointing to another. Now that is what we humans see writing on the command line, however there is much more to understand and learn about the Shell and this command.
On the prompt, you might have noticed how there is a dollar sign ‘$’ at the end it. The reason that is there is to take in a number of parameters or arguments on the command line. It acts like a “Enter text here” box in a way, allowing you to write whatever you’d like on the command line. What the computer also does is break them down into tokens. Tokens are a order of characters that form a word or punctuation. This is how the Shell will check the input, breaking it down and looking through the order of your input. Now onto the steps the Shell takes before executing the command.
Firstly, Shell will take a look of the input, so in this case ls -l and see if there are any aliases. Aliases are essentially shorthand, custom strings that the user can create for abbreviation purposes. If you have a long line of commands on the prompt, you can literally compact that into an alias consisting of two letters and the Shell will execute that command. In this case however, ls -l does not have any aliases. So it moves on to the next step of the background process.
The next step Shell takes is to check if the input contains any built-ins. As the name suggests, these are functions and programs that are built into the Shell to execute directly from the interpreter. ‘ls’ is not a built-in and ‘-l’ is a flag used with the ‘ls’ command to apply long listed, vertical format for it’s output upon execution. Since ‘ls’ is not a built-in, the Shell moves onto another step in the process.
This process is simply checking for any functions and programs that run said command. So in this matter, Shell will look for the functions and programs that run ‘ls’ followed by it’s flag ‘-l’ and run the command. If the input meets the syntax requirements and program checks, it will successfully execute the command and output it’s result. After outputting the result, it will produce a new prompt underneath to allow more more usage of the Shell.
However if the input fails the syntax requirements and program checks, it will produce a error message and then a new prompt underneath; like in this screenshot below:
So to bring this full circle, the Shell performs system calls in order to check input and produce an output on the command line. System calls is just a computer program communicating with the kernel, which is a computer program at the core of the operating system of a computer, when performing tasks and actions. In this case, the Shell is performing system calls to run ls -l on the command line, checking for many pieces before executing. There are three system calls to mention that the Shell uses in this process and those are: fork(); wait(); execve();
So fork is a system call which is used to create a new process called the child process. It runs at the same time with the parent process, who calls the system call in the first place. Both execute the next instruction and share the same program counters, CPU registers, and the same open files as the parent process. However, the child process does not take in parameters and returns an integer. The return values go as follows: Negative Value when creation of the child process is unsuccessful; Zero when the child process is made; and Positive Value that contains the process ID. What does this have to do with the topic? It’s apart of the process behind running a command, which happens in the background of Shell.
The next system call is one called wait. Wait is a system call used to wait on any changes to the state of the child process.
And finally, execve. execve is a system call used to execute the program referred to by the pathname. This is essentially the system call that runs the command you type to the prompt.
I hope this was informative and educational for you! Comment below or e-mail me with criticism, questions, comments and more!