Simple Shell — How it works and what is it.

Juliana Monroy
4 min readAug 22, 2020

For beginning I just want to introduce this little blog about shell. It contains a briefly explanation about what is it and how it works. In here you will find concepts about the main function for shell to work and some examples. Enjoy!

What is Shell?

It is the main user interface to operating systems that are similar to Unix. An operating system is responsible for having several programs run on one computer.

Terminal preview on a Mac

On a Mac or Linux machine, you can access a shell through a program called Terminal, which is already available on your computer. If you’re using Windows, you’ll need to download a separate program to access the shell.

Just remember… The shell makes your work less boring!!

How the shell works?

The shell reads its standard input from your terminal, and sends its standard output and standard error back to your terminal unless you tell it to send them elsewhere.

It is line oriented, it does not process your commands until you press <Enter> to indicate the end of a line.

Reads the command
After we pressed <Enter>

As you can see every time you run a command in your shell the prompt will appear again waiting for another command to be entered.

How is it composed?

→ PID & PPID

A process is an instance of an executing program, that has a unique process ID. This process ID is used by many functions and system calls to interact with and manipulate processes. If you want to retrieve the current process’ ID, getpid().

PID stands for Process ID, Which means Identification Number for currently running process in Memory.

PPID stands for Parent Process ID, Which means Parent Process is the responsible for creating the current process(Child Process). Through Parent Process, child process will be created. If you kill the parent process, the child process is also killed as well.

Process for create a child (When fork() == 0, we found the child)

Once fork successfully returns, two processes continue to run the same program, but with different stacks, datas and heaps.

→ Arguments

The command line arguments are passed through the main function: int main(int ac, char **av);

av is a NULL terminated array of strings ac is the number of items in av av[0] usually contains the name used to invoke the current program. av[1] is the first argument of the program, av[2] the second, and so on.

→ Executing a program

The system call execve() allows a process to execute another program. This system call does load the new program into the current process’ memory in place of the “previous” program: on success execve does not return to continue the rest of the “previous” program.

→ Wait

The wait() system call suspends execution of the calling process until one of its children terminates.

→ File information

The stat() system call gets the status of a file (If it exists or not). On success, zero is returned. On error, -1 is returned.

→ Environment

Environment variables are “stored”. The list is an array of strings, with the following format: var=value, where var is the name of the variable and value its value.

→ Aliases

An alias is a short name that the shell translates into another name or command. Aliases allows you to define new commands by substituting a string for the first token of a simple command. They are placed in the ~/.bashrc or ~/.tcshrc startup files so that they are available to interactive subshells.

Under bash the syntax of the alias builtin is:

“alias [name[=value]]”

Once you have clear the basic, let’s explain what internally happens when we type the command “ls -l” in the shell.

First we entered the command and will validate the PATH to be ready to create a new process with fork() (man 2 fork), as a result we obtain the child process.

After this we can execute our command that will be /bin/ls-l, remember that the PATH before was /bin/ls and with the system call execve() we include the entire command “/bin/ls-l” executed in the child process.

When the function execve() finished we terminate the process with the syscall wait() for the parent, this because we have to wait the correct completion of the child so it can be return to the parent process.

In conclusion we can say that …

The shell splits the line into tokens. A token is a command, variable, or other symbol recognized by the shell. It continues to build up a sequence of tokens until it comes to a reserved word function name, or operator, and will again be waiting for a new command to be entered to execute another process.

--

--