Linux Shell

User interacts with shell and shell talks to kernel and kernel talks to hardware. Hardware does the job. You can write commands or write shell scripts.

Over the years, specific universities and computer vendors made their own modifications to the Linux source code to (hopefully) make the operating system run more efficiently (and faster) on their proprietary hardware. When the vendors sold their hardware, they also distributed (sometimes referred to as “bundled”) their modified version (or flavor) of Linux. All flavors of Linux share basic features, which is an essential concept for the end user to understand.

Shell programs are called shell scripts. Many Linux administrative programs are written as shell scripts.

A Linux shell is actually an interpreter of a programming language. Script is a text file that has

  • Shell commands.
  • Control structures.

When a group of commands has to be executed regularly, they should be stored in a file, and the file itself executed as a shell script or shell program.

Shell

Simply, the Linux shell is a program that interprets and processes the commands the user types at the keyboard, i.e. the command line interface (CLI). When you type a command and press the Enter key, the shell interprets the command, translating and substituting as necessary, then locating, loading and starting the associated program (assuming it is found and displaying errors if it is not).

The shell is not the only user interface since several Graphical UI programs are available. However, the shell is indispensable when interacting with the command line driven programs of Linux.

As is common with Linux, there are a variety of shell programs from which one can choose, each one with its own distinctive features. The shell you interact with will generally be selected by the system administrator when your account is created, but can be changed by the user at a later time. Each shell program will also have its own distinctive command line prompt, but this too can be changed by the user.

Some of the more popular (in a historic sense) include,

Shell NameProgram NameDescription
Bourne Shellshthe original Linux shell created by Steven Bourne, available on most Linux systems
C-Shellcsha shell created by Bill Joy at Berkely, designed to provide some C language-like features
TC-Shelltcsha public domain available extension of csh which allows for command line editing and filename completion
Korn Shellksha shell created by David Korn from Bell Labs which provides features of csh and tcsh with the programming capability similar to sh
Bourne Again Shellbasha shell available from GNU which combines all the best features from sh, csh and the ksh

It is important to keep in mind that the shell program, once started (now a process), remains alive for the length of your login session (unless somehow manually terminated). All commands entered at the command line are input arguments to the shell process.

Shell Interpreter

How does the shell interpret and execute a command, you ask? Simply, the normal sequence of steps is as follows,

  1. The shell is running (i.e. is memory resident) and is waiting for user input
  2. The user enters a command followed by [Enter]
  3. The shell interprets (decodes) any special characters in the command (e.g. meta-characters)
  4. The shell begins to search each directory location specified in the PATH variable for the program (file on disk) the user entered (unless it is built-into the shell; in that case, execution commences immediately). The PATH variable is a variable that contains one or more (usually more) directory specifications. Values assigned to the PATH variable have the form of
dir_spec1:dir_spec2:dir_spec3:dir_spec4

When the shell searches for a program, it searches each directory specification in order; if it finds the program in a directory, the shell quits looking; otherwise it continues to the next directory specication. More on this later in the text. 5. If the program is not found in any directory specification in the PATH variable, the shell reports an error, and returns to step 1 6. If the program is found, the shell creates a copy of itself (via fork system call), almost identical except for a new PID, this becomes the CHILD of the initial shell 7. The requested program is loaded and its process image is overwritten (overlaid) onto the copy of the parent (via exec system call) and execution begins 8. The parent sleeps at this point (wait system call) as the child process runs. When the child process completes, the child dies and sends a termination signal to the parent, which causes the parent process (shell) to wake up and return to step 1

Execute the script

Because of security of files, in linux, the creator of Shell Script does not get execution permission by default. So if we wish to run shell script we have to do two things as follows,

  1. Use chmod command as follows to give execution permission to our script

Syntax:

chmod +x shell-script-name

OR

chmod 777 shell-script-name
  1. Run script as

Syntax:

./your-shell-program-name

Example:

./first

Here ‘.’(dot) is command, and used in conjunction with shell script. The dot(.) indicates to current shell that the command following the dot(.) has to be executed in the same shell i.e. without the loading of another shell in memory.

OR

you can also try following syntax to run Shell Script.

bash first

OR

/bin/sh first

Note that to run script, you need to have in same directory where you created your script, if you are in different directory your script will not run (because of path settings), For e.g. Your home directory is (use $ pwd to see current working directory) /home/workspace. Then you created one script called first, after creation of this script you moved to some other directory lets say /home/workspace/Letters/Personal, Now if you try to execute your script it will not run, since script first is in /home/workspace directory, to Overcome this problem there are two ways

  1. First, specify complete path of your script when ever you want to run it from other directories like giving following command
$ /bin/sh /home/workspace/first

Shell Command Anatomy
Shell Command Anatomy

Subscribe For More Content