What happens when you type ls -l in the shell.

This blog describes the step-by-step of what happens when you type ls -l and press enter in the shell.
For us to write and execute a command like: ls -l in the shell terminal there must be a shell terminal. It seems obvious, but there’s more. In the shell terminal you type the command and start the show. But what is the shell terminal?
The shell.
The shell is a program that allows users to interact with the system by processing commands. Commands invoked from the shell can be classified as internal (they actually correspond to commands interpreted by the shell itself) and external (they correspond to executable files external to the shell). In addition to commands, shells offer other elements to improve their functionality, such as variables, functions or control structures. The set of internal commands and elements available, as well as their syntax, will depend on the specific shell used.
In other words, it is the outermost layer of an operating system, this layer listens to the user through commands and tells the machine what to do executing calls to the system.
General operation of the shell.
Shell language is an interpreted language, in which lines of text (ending in \n) are read, analyzed and processed. The lines to be interpreted are read from:
- The standard entry (default keyboard). In this case the shell is said to be an interactive shell.
- A shell-script file.
- The arguments, with the -c option when running the shell. Example: bash -c “ls -l”
With the lines read, the shell performs the following steps (in this order):
- The lines are divided into different elements: words and operators. The elements are separated using spaces, tabulators and operators. The # character is used to include a comment, which is removed from the processing.
- A distinction is made between simple commands, compound commands, and function definitions.
- Different expansions and substitutions are made (see below). It detects the command to be executed and the arguments that will be passed to it.
- The input/output redirections are performed and the elements associated with the redirections are removed from the list of arguments.
- The executable element is executed, which could be a function, an internal shell command, an executable file or a shell-script, passing the arguments as positional parameters.
- Optionally, wait for the command to finish and save the output code.
When typing commands from the keyboard and trying to enter an item that consists of more than one line, once you type the first line and press Enter, the shell will display the secondary prompt for request for order > (instead of the prompt), prompting you to continue typing the item. When the interpreter completes the input of the element, it will interpret it, displaying the command line prompt again. If you use the ↑ cursor to try to see the command entered, you will generally see how the shell has rewritten the entry to apply the syntax with which the entire element is written on a single line. If you want to cancel the entry of a line (or lines) without deleting what has been written, you can press Ctrl-C.
However there are three important things, initialize, interpret and terminate.
Initialize: The configuration files are usually read and executed.
Interpret: The standard input commands are read at the line prompt and executed them.
Terminate: After executing the commands, executes the shutdown commands, frees memory and ends.
But really the most important thing happens in the Interpret, which implements an infinite loop, which does the following: It reads the command from the standard input, makes a parse separating the command string into a program and arguments to finally execute the analyzed command.
Now focusing on our ls command and contextualizing it within what we just said, ls means “list”, and according to the manual, lists information about the files in the current directory by default.
Split command by tokens.
We enter a string, but for the execution in the shell you need the parts of this line separated. So the shell separates it into tokens. In other words, it separates each word and puts it in an array of strings. You can implement this behavior in getline and strtok.
Check alias.
Bash aliases are those that allow you to set a shortcut command for a longer command. They have the following structure.
alias [alias_name]=”[command_to_alias]”
Bash aliases are those that allow you to set a shortcut command for a longer command. They have the following structure.
After implementing the structure of the alias, when using it, below the shell will exchange the command introduced as alias by the real command with the corresponding arguments for its correct execution.
Finds the command in the PATH.
- Call the program ls with -l
- Fork: creates a child process (pid), when the function is called, for which the operating system duplicates the process being executed, running simultaneously. In order to differentiate them, the original process is called parent and the new process is called child.
- Execve: Performs the execution of the program, for this the executables of the programs replace the parent program that is currently running by the new child program, for this the operating system stops the process, loads the new program and starts it.
- Wait: Stops the parent process while the running child process ends.
When the “ls -l” command is done, print the prompt.
The prompt is the character or characters that indicate that the terminal is ready to receive a new command. Which prints the standard output (Stdout: default file descriptor [1] where a process can write the output.)
Wait for a new command.
In this step of the command execution process the loop starts again, so the shell is waiting for a user input at the prompt.
That’s all.
Notes.
- Expand, Break, Alias, Built-ins, Path, Call, Prompt and Command. E BAB PC PC
- This blog was written for the final project of the first quarter of the Holberton School curriculum for Full stack software engineering.