Part 2 of 6 · 1 min read
The shell
In plain words: A shell is an ordinary user program. It owns no special rights, and the kernel treats it like any other process. A hotel concierge is a fair comparison: the concierge does nothing for the guest personally, but knows how to get the right person to do it. The analogy stops where the concierge waits for the guest to return. A shell waits for the command to finish.
The shell's life is a loop:
- Print a prompt.
- Read a line.
- Split the line into a command and its arguments.
- Start the command as a child process.
- Wait for the child to end.
- Go back to step 1.
A command such as ls -l /tmp becomes an array of strings, {"ls", "-l", "/tmp", NULL}. The first string is the program to run, and the whole array is what the new program receives as its argv. The NULL at the end marks the end of the list, which execvp requires.
Shell
A shell is a user-mode program that reads commands typed by a user (or stored in a script), starts the programs they name as new processes, and waits for them to finish. It adds conveniences such as redirection, pipes and a few built-in commands.
Example: when you type ls /tmp in a terminal, the shell makes a child process, turns it into ls, and prints the next prompt once ls ends.
Check yourself:
1. Does the kernel give a shell special rights?
2. What does the array passed to execvp end with?
3. Which call makes the shell wait for the command?