What happens when you type ls -l in the shell

There are some interesting facts behind it

Thu May 23 2024 - 3 min read

Shell

Let`s begin by defining the shell. In simple terms, the shell is a program that takes commands from the keyboard and passes them to the operating system to execute. It incorporates many features and generally executes other commands.

‘ls -l’ Command Overview

$ ls -l

When we type ls -l and hit enter, it lists the files in the working directory (where we are right now). It shows the file of directory, size, modified date and time, file or folder name, owner, and permissions. After that, the shell prints the prompt again.

Going a little Deeper

When we type the ls -l command and hit enter, the shell reads the entire line from standard input until it finds a newline character. The getline() function is useful for this task.

Now, some magic happens behind the scenes. Once it has read the line, the shell divides the string, adds a null byte at the end of each one, and saves them as different tokens in an array. The first token, in this case, ls, is the filename to execute. It could be a program or aa built-in function, and the second token is -l.

Next, it checks each stored token against previously defined aliases, built-in commands, and the $PATH variable. If the command entered is not an alias, a built-in command, or in the $PATH, it will display a message saying the command could not be found.

Aliases

An alias is a different name that we define and assign to something. For example, we can define an alias for any command. Once assigned, we can type the alias name followed by the enter key to perform the assigned action.

Built-in Commands

Built-in commands exist within the shell and are not external programs in other directories. For example, cd is built-in command. To determine if a command is built-in or an external program, use $ type <name of the commands>.

PATH Variable

In simple terms, the PATH variable tells the shell which directories to search for executables files. The PATH consists of a series of colon-separated absolute paths. In this case, it checks if the ls command is found and matches any executable file.

Now It’s Getting Interesting

If the command is found, it triggers a secondary or parallel process to execute the program. This is called a syscall. Using the fork() and execve() functions, the shell can have parallel processes for the command prompt and the specific command execution. One process waits for the other to finish execution. Once finished, the parent process (the first process created) resumes its process.

After the shell finishes executing the command, it frees all the used memory and prints the prompt again, waiting for more input until we type the exit command and leave the shell.

As you can see, a lot is going on behind typing a simple command in the shell. This is just the starting point for gaining deeper knowledge of how things work once we start interacting with it.

Regresar

Enjoy this note? Feel free to share!

X logo Y Combinator logo LinkedIn logo