pacho0522
5 min readNov 27, 2019

What happens when you type ls -l in the shell

In this blog, we going to write about all the steps, that happen when you type ls in the Linux Shell.

First, is important that you know what is a Shell, so we going to take the words of William Shotts in the book “The Linux Command Line”.

The shell is a program that takes keyboard commands and passes them to the operating system to carry out. William Shott “The Linux Command Line”

So, in other words, Shell is a program that takes commands and interacts with the operating system with a terminal (A text-based interface that allows the communication between the user and the operating system) to make different things, for example, the command ls.

What happens when type ls? (short version)

Well, the command ls in Linux, is for listing all the content of a directory or directories and show the result in the standard output. So when you type ls without arguments, they going to show you all the files and directories that are in the current directory.

The command ls have a lot of possible arguments, the most common arguments are:

-l shows a listing in the long format.
-d shows only the name of the subdirectory.
-t sample sorted by the date of last modification.
-c sample sorted by the date of last modification.
-1 shows the list in a single column.
-i shows the number of the i-node before the file name.
-m shows the files in a line and separated by commas.
-R makes a recursive list ..
-s shows the size in kilobytes of the file in front of the file name.
- color shows each type of file of a different color.
-a shows hidden files.

We going to explain in more detail the command ls with the argument -l, and what happens in the process.

First, we need to know that after the user types the command ls -l, the shell reads the command from the getline () function from the standard input. then they parsing the command into arguments and are executing by the program.

Then, the shell looks for the alias and check if the command ls is an alias. If the command is not an alias, the shell checks if the word is built-in.

Second, the shell going to look for a program with the name ls in the executables files of the system (in the environment), specifically in the $PATH variable.

But what is the $PATH variable?

Well, the $PATH variable is where the shell looking for the directories every time a command is entered (ready-to-run programs). If the command has an executable file with the same name, the $PATH variable going to search into the directory and execute the program.

To execute the program ls, the system makes 3 syscalls (system calls), the first one is the fork, then the execve( ) and finally the wait.

To explain the syscalls we going to use the Linux fork manual, and according to the manual, the description of fork is:

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.

Then we have the execve( ) syscall that his description says:

execve() executes the program referred to by pathname. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

and finally the wait syscall:

wait() All of waits system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal.

The getline( ), wait for the user command, then the shell creates a fork( ) and the Shell looking for the command in the $PATH. After that, the shell executes the execve( ) syscall and executes the command. Then the wait( ) syscall, waiting for state changes in a child of the calling process and doesn’t interfere with the main process.

After ls -l is executed, the shell executes shutdown commands, frees up memory, exits, and re-prompts the user for input.

What happens when type ls? (Long version)

The previous part of the blog shows what happens when you type ls -l in a short version, but we going to go deep in the syscalls and other functions that makes the shell execute the command of ls.

First

The shell going to the function getline( ) function that is used to read a string or a line from the standard input.

Second

The shell makes a strtok to break string str into a series of tokens using the delimiter delim.

Third

In this part, the shell make a syscall to the function fork( ), to create a new process by duplicating the calling process.

Fourth

After the fork( ) in the child process, the shell looks in the environment variables (a named object that contains data used by one or more applications) for the variable $PATH, to see all the directories that have the executables functions.

Fifth

Then, the shell looking for the command that the user type in the getline( ), in the $PATH files, to know if the command exists, is executable and the permissions. In this part, the shell needs to get into all directories to know if the function is there, or don’t exist.

Sixth

In this part, the shell makes a syscall to the function execv( ) to executes the program referred by the user.

Seventh

To finish the process, the shell calls the exit( ) function, to kill the child process.

Eighth

Finally, the shell executes the function and makes a syscall to the function wait( ), to wait for state changes in a child of the calling process and doesn’t interfere with the main process. After ls -l is executed, the shell executes shutdown commands, frees up memory and re-prompts the user for input.