What happens when you type ‘ls *.c’ and hit Enter in your shell?
The Linux Shell is a software or computer program that, through a graphical interface such as a text window, provides the user with the possibility of communicating and interacting with the computer through texts, seeing all the words or commands on the screen. That gives special commands to be executed by the Linux operating system through a keyboard, thus accessing its services and seeing its results.
One of these commands is the “ls” command, in charge of displaying a list of the content (files and directories) of the space or folder of the system, in which the user is located at that moment.
But what exactly happens when ls * .c is typed?
1. When the ls * .c command is entered, the tokenization process occurs first, that is, the keyboard controller recognizes that characters have been entered and passes them to the shell as a single string and this is divided into tokens (portions of text), eliminating the blank spaces, leaving the typed command -in this case- in two tokens: “ls” and “* .c”, and this is stored internally in an array of strings.
2. With the tokenized array, it is searched if each token has a defined alias, if an alias is found, it is saved as a token after removing the spaces as before and again the aliases are verified. Normally aliases are stored in: ~ /.bashrc, ~ /.bash_profile, / etc / bashrc, or / etc / profile which are system folders.
3. It is verified whether each token is a built-in function or not. If the command is built-in, the shell executes the command directly, without invoking another program. For example “cd” is a built-in command but “ls” is not built-in, so the system should search for the executable for “ls”.
4. BASH, is the language that interprets the commands written by the user in text mode or from a file called script, here it interprets the command “ls” and looks for its executable in each location specified in the environment variable $ PATH (in charge of storing the path locations of all common executable programs). This search is done by calling a series of functions such as find_user_command (), find_user_command_in_path, find_in_path_element. In this way, BASH calls the stat () function to verify the existence of this executable in each of the routes.
5. When it finds the file in the location “/ usr / bin / ls”, BASH executes the command execve () to execute its binary file, at the machine level (low level) the program must be read from the disk, its binary format and the proper handling code that will put the binary into memory must be invoked.
6. The program remains in the computer’s memory and from there it is executed, it reads the directories and files on the disk by internally executing a list of functions to read the contents of the directory, invokes a system call, consulting the underlying file system inode (The function used to read the contents of the directory will vary depending on the file system in which the specified path is formatted so that once all the entries have been retrieved, the system call returns.) Finally shell will print the prompt again and it will be saved as PS1 environment variable and the file list will be returned to the prompt.
7. The result on the screen will be the list of all files with a .c extension contained in the current directory where the user is, that is, text files with source code written and developed to compile and execute actions and algorithms in the languages C and C # programming.