Shell Scripting in Linux

Shell Scripting in Linux

An Operating System consists of mainly two components which are- 

1. The Kernel 

2. The Shell  



The Kernel makes the communication between the Hardware and the Software possible. The Kernel is the innermost part of the OS whereas the Shell is the outermost one. 


A Shell in the Linux Operating System takes input from the user in the form of commands, processes the input, and gives an output. It is the interface through which the user works on programs, scripts, and commands. 


There are mainly 2 types of shells in Linux: 

1. The Bourne Shell 

2. The C Shell 


Shell Scripting is the process of writing a series of commands for the Shell to execute. It can combine a series of lengthy and repetitive commands into a single and simple script, which can be stored in memory and executed anytime. 


The steps to writing a Shell Script are as follows: 

1. Create a file using the a Linux editor (such as Vi) and naming the script with a .sh extension. 

2. Start writing the script with #! /bin/sh 

3. Write the required code. 

4. Save the file. 

5. To execute the file, type bash 


Here is an example of creating a simple Shell Script which lists all the files and directories of the OS: 

This script simply executes the ls command of Linux. 


To add comments to a Shell Script, the'#' symbol is used. 


Variables are containers which store data in the form of characters and numbers. Similarly, Shell variables are used to store data but they can be used inside the shell only. 


 For example, the following shell code stores a variable and then prints it: 

Another example: