What Is Shell ? Different Types Of Linux Shell, Shell Scripting Tutorial (Part-1)
A shell is software that provides an interface for an operating system’s users to provide access to the kernel’s services.
On Unix-based or Linux-based operating systems, a shell can be invoked through the shell command in the command line interface (CLI), allowing users to direct operations through computer commands, text or script.
Shells also exist for programming languages, providing them with autonomy from the operating system and allowing cross-platform compatibility.
Operating System Architecture
Before learning about Shell and writing your own Shell Script you must know where Shell resides in OS what’s the role of Shell in OS.
Above image is the architecture of OS, At the Bottom we have Hardware which is basically all the components inside your CPU, on the top of Hardware we have Kernel, it acts as a mediator between the software and hardware, any software cannot directly access the hardware to perform their task they need to pass particular request to kernel.
Simply we can say that, The kernel is a computer program at the core of a computer’s operating system that has complete control over everything in the system.
At the Top of Kernel we have our shell ,shell create an environment through which we can perform various operations.A shell is software that provides an interface for an operating system’s users to provide access to the kernel’s services.
On the Top we have Applications, these are the software installed by users.
Now we know the OS Architecture and where shells resides in OS what’s the role of shell.
Different types of Shells in Linux
Let’s Discuss types of shells we have in Linux, there are various types of shells in linux they are as follows:
Sh: Sh stands for Bourne Shell,A Bourne shell (sh) is a UNIX shell or command processor that is used for scripting. It was developed in 1977 by Stephen Bourne of AT&T and introduced in UNIX Version 7, replacing the Mashey shell (sh).
The Bourne shell is also known by its executable program name, “sh” and the dollar symbol, “$,” which is used with command prompts.
bash: bash stands for (Bourne Again Shell),A Bourne Again Shell is the updated version of sh Shell with more functionalities.
Bash is basically a command processor that typically runs in a text window, allowing the user to type commands that cause actions. It can read commands from a file, called a script. Like all Unix shells it supports the following:
- File name wildcarding
- Hear documents
- Command execution, etc..
csh: The C shell (csh) is a command shell for Unix-like systems that was originally created as part of the Berkeley Software Distribution (BSD) in 1978. Csh can be used for entering commands interactively or in shell scripts. The shell introduced a number of improvements over the earlier Bourne shell designed for interactive use. These include history, editing operations, a directory stack, job control and tilde completion. Many of these features were adopted in the Bourne Again shell (bash), Korn shell (ksh) and in the Z shell (zsh).
Using Different Shells
Now you know different types of shells, Let’s start using some shells, so open your linux terminal window and check what shell you are currently using.
To check the Current shell, type the following command in terminal:
Now let’s see what are the shells available in your system, to do so type following command :
the above command will list all the shells that are available
To change the shell, Simply type the name of the shell you want to use, for e.g if you want to use sh (Bourne Shell) simply type
sh in the terminal.
Executing a Shell Scripts
There are 2 methods from which you can execute shell scripts 1) Absolute Method 2) Relative Method
Absolute Method :
In this method we execute a shell directly irrespective of the location of shell, that means we do not change directory to the directory were our script is saved we directly call it from terminal.
For e.g i have created a shell script in the
/root/Desktop save with the name MyFirstShell.sh, to execute the shell using Absolute method we simply type below command.
Note:Before executing your script make sure you have given executable permission to it.
In this method we change our directory to the directory were the scrip is saved.
To execute script using Relative method, simply change the directory were script is saved and execute script.
Last but not the least you can also check what type of shells are being used by typing following command:
Creating your first Shell Script
Lets create our first shell script, we will be using bash (Bourne Again Shell) so if you are using any other shell switch it to bash.
Below is the code for our first script, it is a simple hello world script, but we will create some awesome scripts in our upcoming blogs .
cat > MyFirstShell.sh
echo "Hello World"
Once you are done writing your script simply safe the script by pressing
TO execute the script type the following command, make sure you have given it executable permission by typing
chmod +x SCRIPT_NAME :
We are done with the basics of Shell Scripting in our upcoming blogs we will dive deeper into Shell Scripting and will also discuss some real world examples .
Below is the continuation of this blog
Keep Coming for More.
Happy Hacking and Happy Scripting !!!