She(ll)

A shell in Linux

Understanding the shell

Shell with commands. Source: JaceTheSaltSculptor
  • In interactive mode -this means connected to the terminal- shell connects to the terminal using [isatty] to open the shell prompt.
  • In non interactive mode -not connected to the terminal-, a command will be executed but the prompt won’t appear.
  1. Getting the command from the prompt: as soon as you enter a command, shell gets all you typed in the prompt (from now on we will call this the string) and keep it in a buffer.
  2. Dividing the string: once the command is in the buffer, the shell divides (or tokenizes) it into smaller strings (tokens) separated by a delimiter, in this case a space. There are some characters that allow you to work with two or more commands in the same line (pipelines, redirection, among others), but for the sake of clarity we will assume there is only one command per line. The first token will correspond to the command and the rest to options, directories, texts, etc. Once this step is done, the shell will check if the input exists in either of these three sections: alias, built-in and PATH.
  3. Checking if there is an alias: the shell will check if an alias or shortname exists, if this is true it will replace the alias with the full path of the command and it will go to step 6, otherwise it will go to step 4.
  4. Checking for built-ins: as the first token corresponds to the command, the shell will check if this token is a part of the shell, if it finds it, this means that the token is not a program, therefore it’s a built-in created along with the shell and will be treated as such. Examples of built-ins are: env, exit, cd, setenv, unsetenv, and more.
  5. Checking in the PATH: If no alias is found it will look for the command into every directory of the PATH variable, in order to do this, the PATH will be tokenized using “=” as delimiter, then the value of this variable (what it is located at the right side of the “=”) will once again be tokenized using “:” in order to separate directories (see photo below).
  6. Making a copy of the program: when a command is found, the shell duplicates itself (this copy is called the child process or subshell) using the fork() system call. The purpose of this copy is to maintain the shell working while another program, in this case a command, is executed; additionally, it is necessary to call wait() system call while the program is running, guess why? Yes, to wait until the program is over. Once this is completed, the shell will go back to the parent process, we mean, the shell prompt.
  7. Executing the program: either the copy is done or not and if the command is correct (if a user enters a path, or just a command, that exists) the program will be executed in the child process using execve() system call (A system call is the way a program enters the kernel to perform some task. Programs use system calls to perform a variety of operations such as: creating processes, doing network and file IO, and much more. A list of system calls can be found by checking the man page for syscalls(2)). But, if a child does not exists a parricide will occur; the parent process will be killed in order to let the child live, so it won’t be possible to execute another program because there is no more shell.
  8. Getting the output: finally, after years of waiting, the user will get the output, and the shell prompt will appear again and again until she types “exit”.
Shell: environment variables

What happens with the command ls -l?

Output

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Configuring Linux AWS Instance

When to use Azure Virtual Machine Scale Sets?

Configuring Our Resolver for Production

images/3_hash_table.png

!鬼灭之刃剧场版:无限列车篇”Demon Slayer: Kimetsu no Yaiba”~HDQ-完整版电影在线”2020!花江夏樹~TW

CS373p Spring 2021: Shan Rizvi

Getting Into the Requirements Meeting

April RoundUp of All Things MoT

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Atenea Castillo

Atenea Castillo

More from Medium

Linux commands

Shell Scripting

Linux on Windows…What?

LVM in RHEL 8 Linux server