Sitemap

Demystifying The Shell

4 min readDec 22, 2016

--

When you open your shell a prompt comes up asking for an input. You then type `ls -l` and then magically, the terminal displays a list of your current directory’s contents in the long format.

But let’s break down the magic of what happens when ls -l is inputted to the shell!

Get Richard Sim’s stories in your inbox

Join Medium for free to get updates from this writer.

Here is a picture of the process

Press enter or click to view image in full size
Flow chart of how the shell works!
  1. The shell checks for signals. “Signals are a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems.” Some example are when you execute ctrl - c. This would send a signal to terminate the current running program. Here is a link on signals.
  2. The next thing, the shell checks if the input is from a pipe, (a file) or from terminal (user input). It looks for the special characters of |, >> and >. since the command that is given to shell is `ls -l`, the shell moves pass this check.
  3. The shell then calls their getline function. This take stdin_fileno (which in our case is ls -l)and reads it into a buffer.
  4. Now the shell will look through this buffer and checks for quotes. If quotes are found then the buffer will treat everything between the quotes as a string. Since there is no quotes in our example the shell will skip
  5. The shell checks for comments which looks for # characters. Since we there is no # in our example, the shell moves pass this step.
  6. The shell then check for variable expansion. This is looking for $ which determine if a parameter expansion, command substitution, or arithmetic expansion should be executed. Since, there is no $ within the buffer, we move past it.
  7. The shell then checks for white spaces and removes excess. Since there is only one spaces between each of the input’s of ls -l the buffer does not need to resize to account for excess white space.
  8. Next, the shell checks for Globbing which looks for wildcards and expands out. An example of this is * where if you type *.c this would expand out and include all the files and directories that end in .c in the current directory.
  9. The buffer is now ready to be tokensized! This mean that that the buffer will be divided into it’s parts using the space as a delimiter. So, our command ls -l now stored in a 2-d array [[ls][-l]].
  10. The shell now takes the first token, in our case [ls] and checks to see if it an alias. The shell will do this by searching in the “shell’s configuration file (`~/.cshrc or the systemwide /etc/csh.cshrc for csh, or ~/.bashrc or the systemwide /etc/bashrc or /etc/bash.bashrc for bash)” . We will assume that an alias hasn’t been set for ls. So the shell will move past this check
  11. Now the first token will check for built-in commands. Since ls isn’t a built in we move pass this step
  12. Now the shell will check the first token for . and / to determine if a directory path is given and execute the file. An example if user input is ./a.out it will look in the current directory for an a.out file and attempt to execute it. However, our first token [ls] doesnt have a . or a /, so the shell will move on.
  13. The shell will now look through the environment to search the environment variable PATH and access the environment variable’s value [display an example of the PATH]. Using the :as a delimiter the shell then appends the our first token [ls] to the end of each field and run the sys call stat . Stat will return 0 if a file is found. So the shell will continue to append [ls] to the end of each of the fields and run the stat call until stat returns 0 or if there are no more fields. In our example, the ls ls executable binary file can be found in /bin/ls.
  14. From there the shell fork’s the current process by creating a duplicate of the process, also know as the child. Each of these process has their own PID(process id). If the parent’s PID is recognized the sys call wait which will make the halt the parent process until the child process closes and sends a signal. If the child PID is recognized the sys call execv will be used to execute the token with the found path, in our case /bin/ls, the second token -l and a copy of the environment. The ls gets executed and sent to standard out with the given flag -l using a copy of the environment.
  15. ls -l will now display on your shell now the child process terminates and sends a signal.
  16. The wait command now receives the signal from the child and allows the parent process to resume.
  17. From here the shell then checks to see if there is any else in the buffer that was filled from getline. An example in which this occurs is if ; was used. In our example, this doesn’t happen.
  18. Finally the prompt will search for PS2 within the environment to search what should be displayed to terminal.

And thats just a glimpse of the magic of how your shell works.

--

--