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

Mhamed Azouzi
3 min readApr 14, 2020

The shell:

is a command language interpreter that takes commands from the standard input and gives them to the operating system to perform.When a program finishes executing, it displays that program’s output.

In other words, it accepts human readable commands from user and convert them into something which kernel can understand.

The kernal

That brings us to the kernal: It’s a computer program that is the core of a computer’s operating system, with complete control over everything in the system. It manages following resources of the Linux system.

The Terminal

Shell can be accessed by user using a command line interface. A special program called Terminal in linux. It is provided to type in the human readable commands.

The Environment

It is an important Unix concept which is defined by environment variables. Some are set by the system, others by the user, yet others by the shell, or any program that loads another program.

The Path tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user.

PS1 “prompt string 1” contains the value of the default prompt.

The alias

An alias is a short cut command to a longer command. Users may type the alias name to run the longer command with less typing.

The command

After understanding how the shell interactes with the Kernal. Let’s get to know the commands and specifically the “ls” command. A command is an instruction given by a user telling a computer to do something, such a run a single program or a group of linked programs. Commands are generally issued by typing them in at the command line.

The argument

An argument, also called command line argument, can be defined as input given to a command line to process that input with the help of given command.

The “ls -l” command

The “ls” command lists the contents (files or/and directories) of the current working directory. With the “-l“ argument it does it in long listing format

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

When you type the command ls -l, the shell collects the input command with the getline() function. It tries to find any matchings aliases and replace it with its value but let’s keep it simple. So it checks if the given command is correct by using the status() function. But running the command the shell needs its executable file, which is located in/bin/ls. But how does it know where the ls file is located? As mentioned before, the environment variable “Path” is the Key:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:usr/local/games.

Once the file corresponding to the typed command is found in these directories, the shell creates a child process using fork() and uses wait()so the parent process wait for the child process to execute. That’s when a call to the execve() system call is made. and that’s when the command is actually executed. The parent kicks in waiting for the new command to be entred.

the output of ls -l

--

--