Shell Implementation: In A Nutshell

Tope Agboola
Jul 20, 2017 · 3 min read

So lets begin, shell we? Shell is a command line interface like Bash which stands for Bourne Again Shell. The first shell, Unix shell, was written by Ken Thompson at Bell Laboratories and published in 1971. However there are other widely used shells for example CSH (C Shell) and SH (Bourne Shell). If you’ve stumbled upon this page I will assume that you know what a terminal is and how to access the shell through it. Whenever you open a terminal emulator a new process begins prompting you with a $ to enter in commands. Shell takes input from the terminal so when you type ls -l and press enter shell is to process the command through several steps in milliseconds.

The shell first stores the input from the user into a buffer and then splits the command ls -l into tokens separated by spaces. It then takes the first token and evaluates if the user input an absolute PATH to where the command is located or just its shortcut name.

PATH is a list of directories that comes along in the environment that the shell searches through. The /bin directory contains the executable program for the commands that aren’t built-ins to operate.

If the command is a built-in a.k.a. built into the shell it is executed immediately. Or if its an alias the user of the computer has set up it gets the command the alias stands for.

If the search through /bin for the appropriate PATH is unsuccessful and it is not a built-in the process is stopped with an error that the command does not exist.

Going forward an existing command will then be executed in a copy of the exact same process. As a defense mechanism shell creates another (child) process in order to run a different program from what is running in the original (parent) process. About now is a good time to talk about system calls. The operating system is responsible for all types of things, file management, starting/stopping processes, memory management, etc. System calls are how a program enters the operating system to perform a task. When we want to create a new process shell uses the fork() system call.

At this point we still need to execute the program now that we have a new process to execute it in. Another system call will be used exec() which comes in many forms all which will run the ls executable found earlier in the /bin folder. Remember those tokens we split ls -l into earlier well now exec() also takes the -l in account as an argument for ls. It is now appended to /bin and exec() now sees it as /bin/ls -l, then exec() will replace the child process with its own then terminate on completion. There is also another system call wait() in place in the background that is waiting for the child process to be done executing so the child process can be closed.

The results are then displayed in long format and all used memory is free’d. The shell is now prompting you for the next command to be taken through the same steps. Nuthing to it!

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade