A quick view inside the UNIX shell

Nick Arshadi
3 min readApr 15, 2020

In this blog, I am going to explain what processes happen when you type ls -l inside your Linux or UNIX Shell.

First of all, what does a Shell stand for? A shell is the medium between the Standard Input/User/Terminal and the Linux Kernel. It is a command-line interpreter.

When you are opening your terminal or running the sh(shell) command, you are entering an infinite loop that only exits when specific commands are passed(Ctrl-d, Ctrl-C, exit).
When opening a new terminal, you create a linked list of all path-names, stored on your computer. Each node of this linked list stores two values. It saves a next-value, which includes a pointer to the next element in the list and a string-value, which consists of a path-name. For each path-name that is stored in your environment variables, a node is created, which stores each path-name separately. I will refer to the importance of this linked list later in this post.

Next, a prompt is getting printed via the write function. The prompt value is in the PS1 variable.
After that, the shell is receiving the user-input via the getline-function. This function checks for stored values in the STANDARD-INPUT file descriptors.

This line, including its ending newline character, will be stored in a string inside a buffer in the heap. In our example, when we type ls -l, we would save the string “ls -l.” But if the memory allocation fails and the return value of the getline function would be -1, the buffer will be freed, and your prompt will be printed again, and you will be prompted to enter new input.

Next, it checks if the string stored in the buffer is equivalent to “exit” via the “strcmp” function. If equal, your buffer will be freed, and your program terminates. If your input does not resemble “exit” we can proceed with the algorithm.

Now your string will be separated into single tokens via the strtok-function. These tokens(strings) will be stored in an array of pointers to these strings. In our case, we started with the string “ls -l” and process it now into two strings “ls” and “-l”. If we had typed in a more extended function, we would receive more strings, but in our case, we receive two strings. This step is necessary because to execute commands that are integrated into the Kernel, we have to use the System Call “execve”. And the syntax of the execve-command needs separated strings containing single arguments(see man 2 ececve).

After that, the algorithm proceeds. It is going to be checked if the first string in our array does exist as a kernel command. This is managed via the stat function (see man 2 stat). In our example, the first string would be “ls”, and the stat command will not find ls. That is because the pathname is missing. Ls is stored under /bin/ls. So if you would type “/bin/ls” stat will return its status.

But we know that typing ls -l also does the job, so how does the shell proceed from here. Now the linked list that we have declared in the beginning comes into use. In the next step, we are going through a loop the size of the linked list, and for every time we pass through this loop, we concatenate the command and the pathname stored in the linked list. After that, we return the status of this new concatenated string. If now one of these strings indeed exists, we store this new concatenated string as our new command. If the status return with “stat” fails, the command remains the same.

After that, we create a child process via fork and execute the command via the syscall execve and use our command and its following tokens as its arguments. So in our case, that would be execve(“/bin/ls, -l, NULL”).

So now, we get the content of our current directory, including filetype, day of change, owner, and the file size of each file/directory in our content. For more information on “ls -l” just type “ls — help” or “man ls” in your terminal.

After that, we free our allocated memory and start the loop from its beginning. I hope you have a great day.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Nick Arshadi
Nick Arshadi

No responses yet

Write a response