Demystifying The Shell
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
- 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.
- 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.
- The shell then calls their getline function. This take stdin_fileno (which in our case is
ls -l)and reads it into a buffer. - 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
- The shell checks for comments which looks for
#characters. Since we there is no#in our example, the shell moves pass this step. - 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.
- The shell then checks for white spaces and removes excess. Since there is only one spaces between each of the input’s of
ls -lthe buffer does not need to resize to account for excess white space. - Next, the shell checks for Globbing which looks for wildcards and expands out. An example of this is
*where if you type*.cthis would expand out and include all the files and directories that end in.cin the current directory. - 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 -lnow stored in a 2-d array [[ls][-l]]. - 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 - Now the first token will check for built-in commands. Since
lsisn’t a built in we move pass this step - 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.outit 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. - 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 callstat. Stat will return0if 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 returns0or if there are no more fields. In our example, thelsls executable binary file can be found in/bin/ls. - 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
waitwhich will make the halt the parent process until the child process closes and sends a signal. If the child PID is recognized the sys callexecvwill be used to execute the token with the found path, in our case/bin/ls, the second token-land a copy of the environment. Thelsgets executed and sent to standard out with the given flag-lusing a copy of the environment. ls -lwill now display on your shell now the child process terminates and sends a signal.- The
waitcommand now receives the signal from the child and allows the parent process to resume. - 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. - 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.




