What happens when you type ls -l in the shell

Diego Monroy
4 min readAug 27, 2019

--

There are some interesting facts behind it

Screenshot by Authors 2019. shell prompt example.

By Geraldinne Bohórquez and Diego Monroy.

Let's begin by defining the shell; in simple words, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. It incorporates many features and generally executes other commands.

ls -l (command) on the surface

Screenshot by Authors, 2019. ls -l command example.

When we type ls -l and hit enter, It is going to list the files in the working directory (where we are right now) showing file or directory, size, modified date and time, file or folder name and owner of the file and its permission, and finally prints the prompt again, did you see the $ sign at the end?, the shell is going to prompt the PS1 which is an inbuilt shell variable that stands for “Prompt String One” or “Prompt Statement One”, the first string that we are going to see, if you want to know what PS1 variable looks in your machine and how is the format of the prompt, you can type echo $PS1 .

Going a little bit deeper

When we type the ls -l command and hit enter, the shell is going to read all the line from standard input until a newline character is found in order to perform the task required, for this, the getline()function comes helpful.

Screenshot by Authors, 2019. ls -l example.

Now It is going to perform some magic behind the scenes, once It has read the line, internally It's going to divide the string and add a null byte at the end of each one and save them as different tokens in an array, the first one, in this case, ‘ls’ , It is the filename to execute, It could be a program or a built-in function, and the second one, in this case, ‘-l’.

After that, It going to check each stored token against defined aliases that we have defined previously, then built-in commands and of course, our $PATH variable, in case the command entered is not an alias, or a built-in or is not in the $PATH and it can not be executed, it will show a message that it could not be found.

Aliases

An alias is like a different name that we define and assign to something, so, for example, we can define an alias for any command, once the command has been assigned to its alias, we can type in the alias name followed by the enter key to perform the action assigned to.

Built-in commands

Build-ins are commands that exist within the shell, they are not outside programs living in other directories, so, for example, cd is a built-in command, if you want to know if a command is built-in or an external program use $ type <name of the command> and it will tell you what type of command it is.

PATH variable

Screenshot by Authors 2019. PATH variable.

In simple words, the PATH variable tells the shell which directories to search for executable files, the PATH consists of a series of colon-separated absolute paths and in this case, is going to check if the ls command is found and match any executable file.

Now It's getting interesting

If the command is found is going to trigger a secondary or parallel process to execute the program, this is called syscall, using the fork() and execve() functions is possible for the shell to have parallel processes for the command prompt and for the process of executing the specific command, so one process is going to be in stand by or waiting for the other process to finish with the execution, once is finished, the parent process, as Its the name given to the first process created, would resume its process.

Once the shell finishes executing the command that we type in, it is going to start all over again but first is going to free all the memory used and prints the prompt again waiting for more input until we type exit command and exit the shell.

As you see there is a lot going on behind typing a simple command in the shell, and it is the starting point of getting more deep knowledge of how things work once we start interacting with.

--

--

Diego Monroy

Frontend Developer💻, Holberton School 🎓, Zen ➡️ Code ➡️ Read ➡️ Debug ➡️ Repeat, Peer pressure can change opinions and even perceptions💡