Typing command-line into shell

Salah Besbes
6 min readNov 25, 2020

happens when you type “ ls -l ” ??

Shell : provides a command line user interface for Unix-like operating systems ( OS ). It’s a link between user and the OS, thanks to the command-line written by the user. It’s role consist of :

  • Read command-line
  • Interpret it
  • Execute command
  • Print the result to the stander out-put

In fact the Shell is an executable program that read command-line, execute some program/function and return a result to the user.

There are too many types/versions of shells we list:

  • 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

what we should know from this image ?

sh (Bourne Shell) is the oldest version of the shell, it is installed on every Unix OS, but it’s poor on functionalists comparing to other versions.

bash (Bourne Again Shell) is an upgrade of the sh, and it’s one used by default on almost all Unix OS.

What’s a process ?

A process is an instance of a program in memory. It’s a sequence of instructions and each process has a block of controlled data associated with it. Processes can be manipulated in a way similar to how files can be manipulated. On every program lunch, there are 3 file descriptors opened :
stdin , stdout, stderror.

The Linux Standard Streams

stdin ( 0 ) : standard input stream. This accepts text as its input.

stdout ( 1 ) : Text output from the command to the shell is delivered via the
( standard out ) stream

stderror ( 2 ) : Error messages from the command are sent through the (standard error ) stream.

what’s an aliased program ?

An alias is a name and corresponding value set using the alias builtin command. The shell checks the word to see if it matches an alias. If it does, it replaces it in the input stream with its value. For example:

aliases variable are found in : ~/.bashrc

how many type of command the shell can execute ?

there are 2 major types :

Shell Built-ins commands : Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command , the shell executes the command directly, without invoking another program or invoking any child process.

Normal Programs: Are programs found in one of the directories in the PATH environment variable each 2 directories are separated bu colon ‘ : ’

/usr/local/sbin   and usr/local/bin  are both directories containing normal programs used by the Unix 

to know the type of the command we can use type.

So what happen when the shell execute ?

Read command-line

  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

Interpret command

  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 .

Execute command

  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()

what’s fork() and exec()

The fork system call is used to duplicate working process (the parent). This newly created process is known as child process.

A child process uses the same program counter, CPU register, same files that are used by the parent process.

The exec family of functions replaces the current running process with a new process. It can be used to run a C program by using another C program

Exit Status

  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.

Let’s take an Example: ls -l

  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.

--

--