Typing command-line into shell

  • Read command-line
  • Interpret it
  • Execute command
  • Print the result to the stander out-put
  • sh : Bourne Shell
  • bash : Bourne Again Shell, This shell can be found installed and is the default interactive shell for users on most Linux systems.
  • ksh : Korn Shell, based on the Bourne shell sources
aliases variable are found in : ~/.bashrc
/usr/local/sbin   and usr/local/bin  are both directories containing normal programs used by the Unix 

So what happen when the shell execute ?

  1. The program reads the command-line given by the user until it found ‘\n’.
  2. search for comment and delete it. ignore what came after ‘ # ’
  3. search for ‘ space’ to split it into multiple words and save them some where in memory.
  4. It check the first word of the line if it a reserved word, “reserved word are ”
  5. if it is not a reserved word, then the shell has recognized a simple command. First thing to do it check if it is an aliased command name and replace it.
Reserved words in sh
  1. Leading words of the form “name=value” are stripped off and assigned to the environment of the simple command. Redirection operators and their arguments are stripped off and saved for processing.
  2. The remaining words are expanded, the first remaining word is considered the command name, the remaining words are considered the arguments of the command
  3. Then it handle redirection if found .
  1. The shell first looks for built-ins functions, if the command name correspond to one the name of its built-in function, it takes as arguments all remaining words typed and the environment variable array.
    Shell built-in function is executed internally to the shell, without spawning a new process.
  2. If not found, the command is searched for as a normal program in the file system, two things happens:
    * Command names containing a slash are simply executed without performing any searches.
    * The shell searches each entry in the environment variable PATH in turn for the command. The value of the PATH variable should be a series of entries separated by colons. Each entry consists of a directory name containing executable programs.
  3. if found, the program execute it using fork() and exec()
  1. Each command has an exit status that can influence the behavior of other shell commands. The paradigm is that a command exits with zero for normal or success, and non-zero for failure, error, or a false indication.
  2. Any message to print in case of error, is printed in the stderror , On sucess any message is printed in the stdout.
  1. No ‘ # ’ detected, nothing to ignore.
  2. The program splits the command-line into separate words
  3. It check the first word of the line if it a reserved word, “reserved word are ” -> not out case
  4. First this is to check for aliases for ‘ls’ if found it replace it
  5. nothing of the form “name=value” is typed as first word -> nothing to add to environment variables and there is no redirection sign
  6. Then all words found are expanded, ( there are only one command and no pipes or special parameter found ). The program consider the first word found is a name of an built-in function or a Normal program and the remaining words are its argument (whose starts with ‘-’ are options and all others are arguments).
  7. Now, the shell search for a built-in function to execute if it name matches the command name typed, and takes all argument typed and the environment variables .
  8. ls is a program so it wont match any built in function. Then since “ ls ” doesn’t contain any ‘ / ’, the program search for it in each directories is the environment variable PATH, ‘ : ’ separate in between 2 directories.
  9. In our case “ /bin/ls “ is the path of an executable program. In that case the path found and the arguments typed and the environment variables are passed the one of the exec family to execute the program, meanwhile the parent wait for the child to finish executing ( using await() )
  10. The program execute with the option “ -l ”
  11. When it finish execution, it exist with a status 0 (work as expected) or some thing else if not.

--

--

--

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

Recommended from Medium

Dubsado vs. 17hats: The CRM Showdown

Found This Week #41

HashiCorp Consul: Multi-Cloud and Multi-Platform Service Mesh

Flutter is no longer a cross-platform framework

Merge k Sorted Arrays

Automations Concepts Overview for No-code Tools

Emergent Enterprise Architecture

Introduction to Packer

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
Salah Besbes

Salah Besbes

More from Medium

SharePoint Framework Development on a $99 Raspberry PI

How to bypass Windows 10 UAC with Python

NTP on Oracle Linux 8

Install Arch Linux on Kvm