Updated June 29, 2023
Introduction to Shell Scripting
Shell Scripting is a text file containing the sequence of commands in the UNIX operating system. Shell scripts perform various tasks like file manipulation, program execution, text printing, disk backups, and evaluating system logs, etc. It is also used as an installation script for complex programming languages. Shell has some parameters to perform certain operations and is helpful for repetitive tasks.
The operating system is a bridge between computers and their users and a foundation for all other applications. The two main components of the operating system are:
- Kernel
- Shell
A Kernel is a link between the computer operating system and hardware. A kernel is the core of an operating system. It manages all core memory usage. The types of kernel are Micro kernel, Monolithic kernel, etc.
A Shell is an intermediate between the operating system and the terminal. It is the interface between the kernel and the user. It acts as a command interpreter which converts the information from the user (Human language) into machine-understandable language.
Types of Shell
There are two types of shells in Unix and Linux which are as follows:
1. Bourne shell
The Bourne Shell is known as the ‘sh’ shell. The default prompt for this shell is $. It is most popular in the Unix operating system. The supersets of Bourne Shell are Korn Shell and Bash (Bourne Again Shell). Bash is free and most widely used.
2. C shell
The C shell is known as the ‘csh’ shell. The default prompt for this shell is %. The derivative of C Shell is Tops C shell, known as tcsh. The sub-category of the c type is the TENEX c shell.
The shell script has the same syntax as any other programming language. Knowledge and experience in programming languages like C, C++, Java, and Python are essential to learn and write shell script programs. It comprises keywords like if, while, break, and else and shell commands like ls, cd, echo, move, etc. The function to perform specific tasks and constructs or control flow are if..then..else, while loop, case, \, and so on.
Why do we Need Shell Scripts?
We need to write shell scripts to avoid repetitive work and automation, perform routine backups by system administrators, add new functionality to the shell, and so on.
How does Shell Scripting Work?
The shell scripting program contains a series of commands to execute.
Steps to create Shell Script:
- Create a shell script file using a vi editor or any other editor. Give the file name with extension .sh
- Start the script with #! /bin/sh
- Write the required code and save the file.
- Execute the script file by typing bash file.sh
How to Write the Shell Script Program?
The uses of programming components like variables, control structures, and more are as follows:
The “#!” operator directs the script to the shebang interpreter location. So, #! /bin/sh means the script is directly executed to the bourne shell.
Let’s see the script example where it uses the ls command. The ls command helps to retrieve the list of files and directories in the current directory. The code is file.sh file like:
#!/bin/sh
ls
When we run the script file.sh file, the ls command will run.
Commenting is a non-executable code that is important in any program. In Shell programming, the # symbol is used to add the comment.
Consider the following example:
#comment line is here
The Shell Variables store data or constant values, which can be of characters and numbers data types. Let’s look at an example of a variable using the name variable to store “John” name and then printing the name variable value. The variable value is displayed using the $ symbol. The echo command displays a line of text/string on standard output or a file.
name ="John"
echo $name
The read command reads a line of text from standard input. The following example is of a reading command written in the file.sh file.
#!/bin/sh
echo "what is your name?"
read name
echo "Hello, $name"
Then the file.sh run as:
$./file.sh
What is your name?
John
Hello, John
This is a simple script. Likewise, we can develop advanced scripts containing conditional statements, loops, and functions.
Who can Learn Shell Scripting Technologies?
Linux users and professionals who want to automate tasks using any of the shell scripts and those who want to learn a simple programing language.
Advantages and Disadvantages of Shell Script
Given below are the advantages and disadvantages mentioned:
Advantages
- Shell scripting is simple and efficient to use.
- The command and syntax are the same as that of other programming languages.
- The programmer can quickly write and start the shell scripts, interactive debugging, etc.
Disadvantages
- The execution of the shell script is slower compared to other individual commands.
- Other disadvantages are minimal data structure, unlike other scripting languages, difficulty remembering all the commands, complex flaws in the design, etc.
- Any errors in shell scripting can prove quite costly if left unnoticed.
Scope
It is helpful in many organizations to write workflows for ETL, epsilon, and other tools to save time. The pay scale is good for scripting individuals and increases with skills and experience.
Conclusion
A Kernel is an intermediate between the operating system and hardware. A Shell is an intermediate between the operating system and the terminal. The Bourne and C shells are the most widely used shells in the Linux operating system. Shell scripting program contains a series of commands for the shell to execute and store data. It can help to create complex programs containing conditional statements, loops, and functions. The target audience for shell scripting is Linux users and professionals. The scope of the shell scripts is very useful in many organizations to save time.
Recommended Articles
We hope that this EDUCBA information on “What is Shell Scripting?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.