Topic 1: Introduction to the Shell Scripting
Introduction to the Shell Scripting
Tech informant is happy to launch a new section for our readers, i.e. Shell Scripting. In this article, we give the basic introduction about the shell and Shell Scripting importance in the Linux operating systems.
What is a Shell?
The shell is the most important component of the operating system which is transparent to a user. It acts as an interface between the user and the operating system by intercepting the commands entered as inputs.
When the command requires the utility, the shell communicates with the kernel to run the utility, upon getting the response, the shell gives the result back to the user.
Role of a Shell
As an Interpreter: Like we mentioned above, interpreting means taking the commands to the kernel for execution.
The above diagram shows that user interacts with the kernel through the shell.
As a programming instruction: Apart from the interpreter, the shell has very good programming capabilities which allow writing a script. Well, we are getting close to the topic, the ‘script’. Yes, Shell Scripting.
A ‘shell scripting’ is a combination of commands collected and saved in a file. We will talk more about shell scripting in the next coming topics.
History of Shell
Different shells have designed in the past, which are still in practice. Few of them are
1. The Bourne shell, developed by Steve Bourne
2. The Bash Shell, which is an extension of Bourne shell, in short, we called bash as Bourne again shell which is widely used in the Linux.
3. The C shell, developed by Bill Joy
4. The Korn shell, developed by David Korn, in short, we represent it as ksh.
Let’s get start with the Shell Scripting.
Suggestable Read : Introduction of Linux and Installation steps & Basic Configuration
Is it not a tedious task to remember all the commands at the time of implementation? Surely you would say yes. But the beauty of scripting is it automates all the tasks which have to be done manually every time. Pretty Cool, Right?
A shell script is basically a file which contains a collection of commands when executed runs all the commands to perform a particular task. Sometimes a shell scripting is called as shell program. Basically, it is a small function that is interpreted, but not compiled as the shell is an interpreter.
How to write a script?
To write a script, we need a command prompt and terminal. ‘$’ is the shell prompt we use for writing a shell.
For Ex: type the command ‘echo $SHELL’ without quotes in the terminal and press enter. Echo is the command which returns whatever we write, and $SHELL specifies what shell we are running currently in.
/bin/bash (Here the shell running is bash)
We can write a script to a text file and save it with an extension ‘.sh’. There are few editors available for writing a script, but we write a standard script on the vi editor and save it with the extension.
Sample Script 1
In the above example, we have created a script hello with a .sh extension. Once it is created, we need to change the file permissions of it.
|chmod 755 hello.sh|
Once the script is done, we need to check the output. We can execute the output by running the command ‘sh hello.sh’ which will give the output by printing the ‘Hello World.’
In the above program, the first statement is #!/bin/bash which we call it as shebang. Because ‘#’ symbol is called hash and ‘!’ is the bang. Hence it is shebang.
It indicates that whatever commands that are followed in the script are executed by bash shell or the bash is executing the script.
Sample Script 2
In the above code, we read the name that enters on the command prompt and whatever name is entered that is displayed back on the screen by running the script.
We can run the above output with ./hello1.sh as we have created with a .sh extension or you can use sh hello1.sh. Either way,the script works.
Conclusion: In this topic, we have seen the introduction to the Shell Scripting, types of shell, how to write a script and process of execution. In these next coming topics, we will get into the depth of scripting. We welcome your valuable suggestions and comments. Your positive feedback helps us grow, perform better and deliver our best.