Writing a shell in python
UNIX is very simple, it just needs a genius to understand its simplicity. — Dennis Ritchie
What is a shell?
Already a lot of definition online, so a simple one would be:
A shell is an interface that allows you to interact with the kernel of an operating system.
Some examples of shells are:
How does a shell work?
A shell parses commands entered by the user and executes this. To be able to do this, the workflow of the shell will look like this:
- Startup the shell
- Wait for user input
- Parse user input
- Execute the command and return the result
- Go back to
2
.
There is one important piece to all of this though: processes. The shell is the parent process. This is the main
thread of our program which is waiting for user input. However, we cannot execute the command in the main
thread itself, because of the following reasons:
- An erroneous command will cause the entire shell to stop working. We want to avoid this.
- Independent commands should have their own process blocks. This is known as isolation and falls under fault tolerance.
Source code