Shell Step By Step : What Happen When You Type ls -l In The Shell

Ahmed Belhaj
4 min readNov 22, 2020

--

ls -l Output to STDIN(STANDER STREAM)

Introduction

Hello again , three month back i wrote an article explaining what happen when you type ls *.c and hit enter i explained mostly that's you listing c files and nothing about the behind mechanism of shell and how it really work . Three month passed I learned a lot typically most of the c programing language and basic of shell .

My Adventure Creating Simple Shell

Well last 13 Day , I Was Creating a Simple Shell As School Project For Holberton School A Nice Experience and kinda not easy thing to do but I Did it anyway . So lets dive into what really matter.

Shell

Shell is is a command-line interpreter , The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts. User typically interact with it using interface known as Terminal . And We gonna explain how the shell interrupt the user input and execute it .

1- Split The Input

First thing that shell when you do enter input he will parse the command you enter if it have multiple word like ls -l /var the shell split ls alone and -l , and finally /var alone and remove the return to line (enter) explained as \n in programing language and also remove any redirection in the input

Commands and Built-In

after the shell removed everything the first word in the beginning(left) is assumed to be a command name

Redirection (ls >out) is done first and it never been in the command name .

then shell check if the command name is built-In and if its true then its executed prompt again .

Different Between Them

Commands are executable program file , shell search for them and after he execute them .

Builtin are executed automatically by the shell and they are external program

e.g echo cd, history , help ect.Some Built-in command are also have external version, so they are both builtin and excutable.

2-Searching for the command name in the Path

So after we typed ls -l and parsed ls alone and -l alone .so ls -l is not a builtin so its assumed directly by the shell that's its is a command name and its excitable file so the shell will attempts to find the excitable file with the name .

if the command name contain no slashes like ls the shell the will look into the environment variable called path

How To Display PATH

The environment variable is mostly exported and inherited by the child process of your shell .Directories in the path are separated by : as shown above image .so when you type a command without slashes ls -l for example the shell go looking for it in those directory but if you type a command with slash then the shell will assume that's it already a path to and executable and will not go looking for it for example /bin/ls .

in case of the ls the shell will go looking for it ,he will search for a file called ls in those directory left to right and run the first executable program with matching name . in the above example we can see that the first directory usr/local/sbin so it will be searched first the the next and next (usr/bin)is mostly the directory containing the executable

if the command name appears multiple time in the path directories the first one will be executed. if the command is not any of the directory of the path the shell tell you that its not found

Example from The Shell I Created

Case that you input a valid command the shell then Execute the command.

3-Executing the Command(Fork,Wait,Execute)

After That the shell Use A System Call Called Fork in c programing language this function create a new process from the parent and use it to excuse the another case in our case it ls so as we mention before commands are program so they occupied process when executed . in case the parent process of our shell working we need to suspend it for a second execute the other process the the shell return working again . in here we mostly using system call .system call interfere with the Kernal .

when the execution happen the system call that provide the execution take all the other argument from the input like -l argument in our case into the other executable program (ls).

Thank You For Reading Fill Free to check My Simple Shell Project.

https://github.com/Theemiss/simple_shell

--

--

Ahmed Belhaj
Ahmed Belhaj

Written by Ahmed Belhaj

Dedicated DevOps Engineer with expertise in system administration, core development, and a full-stack software development.

No responses yet