Simple Shell

Christian Suárez
4 min readNov 27, 2019

--

What happens when you type “ls -l” and hit Enter in a shell.

First of all, we must define what is a shell

A shell is a user interface for access to an operating system’s services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is the outermost layer around the operating system kernel.

in our case we are using a CLI, which requieres certain level of knowledge of the shell commands, and their syntax.

to use the CLI we need a terminal, this terminal is the one that takes what we type with our keyboard, and connects those inputs to the shell.

the shell prints a $ symbol and waits for out input, the prompt or command line is where we will type your command.

So… ls -l

Let us focus on the “ls -l” command, this command lists all files and directories in the current working directory, or the one we send by parameter.

“ls” has some built-in options to make our search more effective or specific

some of the options are:

  • l — List files and directories in Long Listing Format.
  • a — To check hidden files and directories.
  • i — To check Inode Informations.
  • n — List UID and GID number of Owner and Groups to which the files and directories are belongs.
  • g — List group of files and directories to which they belongs.
  • G — List users and not groups of files and directories to which they belongs.
  • h — List the size of the files and directories in Human Readable format.

but the objective of this blog is to know what happens when we use the flag

‘-l’, so let’s see what is the result of this command:

HolbiPro$ ls -l
total 312
drwxr-xr-x@ 13 holberton staff 416 Nov 26 12:13 0x15.c-master
-rwxr-xr-x 1 holberton staff 125 Nov 25 12:06 AUTHORS
-rw-r — r — 1 holberton staff 526 Nov 22 10:08 wait.c
HolbiPro$

when we type the command in our terminal ( what we use to interact with the shell and type our commands) and press enter, this is what happens under the hood:

First: our command enters as a long string “ls -l”, the shell takes it and separates word by word making a token out of each word, resulting something like this (just a visual interpretation):

Token0 = ls

Token1 = -l

then s our command is now tokenized, a function in our shell takes the Token0 and Token1 and binds them to the /bin/ path (PATH (path, route) is an environment variable. Environment variables contain information that is accessed through the variable name (as in programming languages).

If we happened to have typed /bin/ls -l, then the previous operation would be ignored and would proceed to the next step.

Once this operation is done, we get something like this “/bin/ls -l”, this happens internally, we don’t actually see this happening.

Another function in our shell, looks in the UNIX KERNEL, if there is any match with the “/bin/ls”, if it does it will then search if “/bin/ls -l” matches any option of the /bin/ls in the KERNEL, then as our shell now know what are we asking to do, a fork function creates a child process and then executes that order in the new process, prints the result in our terminal so we can access that information and kills the child process. (fork creates a little process, because once the command is executed, it closes the process, and as we don’t want out shell process to be closed, we create another one just to execute the order and then when it is killed, it comes back to the shell process with no issues).

Once the child process is killed and the information is printed in our terminal, all memory blocks being used until this step are freed and the shell prints a new $ symbol, stating that it is ready for the next order.

--

--

Christian Suárez
Christian Suárez

Written by Christian Suárez

0 Followers

Software developer under construction @Holbertoncol