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

Julian Villegas
Basics Full Stack Engineering

--

Keep in mind that you need some previous knowledge of how a Unix shell work to understand this article.

First, we need to know what is a shell, a shell is the command interpreter between the user and the computer (yes is a black box full of letters with different colors), when you write a command on the terminal, the shell translates the message to the computer and then the command is executed. Include a command prompt, is a short text message at the start of the command line on a command line interface. (See below — print command line (PS1) and $)

The shell is like an infinite loop printing a prompt waiting for the user to enter any command or input, at the moment that you write ls -l the shell is going to take those parameters through some functions.

The first function is going to divide those parameters into two separate strings of text or “tokens” (token1 = ls and token2 = -l) this is useful since the first token indicates the command that we want to execute and the second is going to be the arguments of that command. See man page for synopsis and different arguments.

Then the shell is going to check if the first token is an existing command saved in the PATH (the path is a list of directories where the commands, utilities, and programs are saved). This environment variable is shown, for example in bash by echo $PATH. Contains a colon-separated list of directories that the shell searches for commands that do not contain a slash in their name.

Before that, shell use the system call fork that is used for creating a new process, which is called child process which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

When fork return the process id (pid) of the parent, shell makes a call to wait() that blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.

After fork process, if the command is found, the shell is going to use the system call execve, this system call is going to execute the command and the parameters.

Alias expansions

The shell identifies the first command, and checks if there are aliases for the specified command. So, What is an alias?

An alias is a command in various command line interpreters (shells) which enables a replacement of a word by another string. It is mainly used for abbreviating a system command.

If an alias is found, the shell checks if there are any aliases for that alias.

Print command line (PS1) and $

The environment variable PS1 is used for the format of the prompt. Is the default interaction prompt for the command line: it displays the username, hostname, and full path name of the user directory. The last step after running the ‘ls -l’ command is printing the command line prompt (PS1) with a line break.

Reference: https://www.geeksforgeeks.org/introduction-linux-shell-shell-scripting/

--

--

Julian Villegas
Basics Full Stack Engineering

Professional System Engineer and Student at Holberton School Colombia