Do you know how the Linux shell interprets a command?

If you are knowledgeable about the Gnu/Linux operating system and you use the terminal, maybe you already know what is a shell, but do you know how it’s engineering process is behind it? ¡Today you will learn how!.

if you want a quick answer here a diagram (algorithm of the entire process of executing a command).

Well, if you keep reading this post it is because you want to know the full explanation of how it’s algorithm works, so let’s get started.

Before we begin we must understand these two terms.

user input: Text entered by the user using the terminal

Environment: General system variables that are used by system programs.

PATH: environment variable that stores each path where the executable files are.

Two fundamental (main) data must be taken into account in the process of interpreting a command. Environment variables and user input.

The shell starts waiting for a keyboard input from the user (this is known as standard input)once the user enters a text (because it is not yet taken as a command but as a text like any other), the interpreter proceeds to “Tokenize” the text entered by the user (separate the user input into smaller parts, divided by the space character (“ ”).

Once the shell has “Tokenized” this text string, it will proceed to search among all the environment variables that the system has, in search of the environment variable “PATH” (go to the beginning of the post to find out what saves this variable)

Once it finds it, it will proceed to “tokenize” this route but this time the delimiter of each token will be “:” so, /usr/local/bin will be a token and /usr/bin will be a different token. Once the “tokenization” process is finished, the shell proceeds concatenated the environment tokens and the first token of the user input, this way.

It will concatenate the first “PATH” token with the first token entered by the user. Let’s do an example with the command “ls”

the concatenation will be as follows:

/usr/local/bin/ls

It will proceed to verify if this route exists, if it is not the case, it will iterate again and concatenate the second token of “PATH” with the first token of the user input, like this:

/usr/bin/ls

and will do the same verification again.

In the case of the “ls” command, it would enter the third iteration, since the “ls” command is located in the /bin/ls path.

Once the shell is sure that the command exists, it proceeds to create a child process (a clone of itself (of the main code)) to execute that file (/bin/ls) to this execution it will be passed as parameters of the main program (/bin/ls) the remaining tokens entered by the user, once this command is executed the child process will die and return to the main program (at the prompt (where the person writes the text)) and again awaits a keyboard entry by the user.

In the event that there is an error when executing the command in the child process, the child process will return an error, saying that there was an error executing the command.

In case the command was not found (“ls” is not in any of the PATH paths) it will return an error like this.

“Command not found”.

and that would be the end, this in few words but very concise is the algorithm to execute a command.

Follow me on my digital social networks for more posts like these.

linkedin:https://www.linkedin.com/in/andr%C3%A9s-f%C3%A9lipe-sepulveda-jimenez-92657b1a0

Twitter: https://twitter.com/andress14023892