Shell Scripting is an open source computer program designed to be run by Unix/Linux shell. shell. Shell Scripting is a program that writes a series of commands for the shell to execute. It can combine long and repetitive command sequences into a single and simple script that can be stored and executed at any time.
| Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
In this article, you will learn the basics of Linux/Unix shell scripting programs to advanced concepts of Shell Scripting. The article is designed for beginners and experts who want to learn what is Shell Scripting? How shell scripting works, types of shells and more.
What is Shell?
Shell is a UNIX term that refers to the interface between the user and the operating system service. The shell provides the user with an interface and accepts commands that the user puts into the system and executes those commands automatically, providing the results of the program in a shell script.
An operating system is made up of many components, but its two main components are:

Kernel is the nucleus of the computer. It helps the system communicate between hardware and software. While the Kernel is the innermost part of the operating system, the shell is the outermost part.
The shell in the Linux operating system receives input from the client in the form of a command, processes it, and then outputs the results. It is the interface through which users can work on programs, commands, and scripts. The shell is accessed using the terminal.
When you run the terminal, the shell issues a command prompt (usually $)where you can enter a command, which will then be executed when you press the Enter key. The output or result will then be displayed on the terminal.
The Shell wraps around the delicate insides of the Operating System, protecting it from accidental damage. Therefore it is called Shell.
Types of shells
There are two main types of shells in Linux:
- Bourne Shell: The prompt for this shell is $ and subshells include:
- POSIX shell is also known as sh
- Korn Shell is also called sh
- Bourne Again Shell is also known as bash (most commonly)
- C shell: The prompt for this shell is % and different types like
- C shell is also known as csh
- Top C shell is also known as tcsh
We will discuss shell scripting based on bash shell in this article.
How to write Shell Script in Linux/Unix
Shell Scripts are written using text editors (text editors). On a Linux system, open an editors program, open a new file to start typing a shell script or shell programming, then give the shell permission to execute the shell script and place your script in a location where the shell can find it.
Steps to create Shell Script:
- Create file using vi editor (or any other text editor). The name of the script file must have the extension .sh
- Start the script with #! /bin/sh
- Write a few lines of code
- Save the script file as filename.sh
- Execute the script file using the command bash filename.sh
“#!” is an operator called a shebang that directs the script to the interpreter location. So if we use “#! /bin/sh”, the script will be redirected to bourne-shell.
Let’s create a small script:
#!/bin/sh
ls
Steps to create Shell Script program in Linux/Unix:

The ‘ls’ command is executed when we execute the sample.sh script file.
Commenting is a very important element in any program. In Shell programming, the syntax for adding comments is, the content after the mark # will have no value, the purpose is to explain the author’s code.
#comment
For example:

What are variables in Shell?
As discussed earlier, variables store data in the form of characters and numbers. Similarly, shell variables are used to store information and they work only in the shell.
For example, create a shell variable and then print it out:
variable ="Hello"
echo $variable
Below is a small script that will use the variable:
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
Steps to create and execute script:

As you can see, the program has chosen the value of the variable ‘name’ as “Joy” and ‘remark’ as “excellent”.
This is a simple script. You can develop advanced scripts containing conditional statements, loops, and functions. Shell Script will make your life and Linux administration easier.
Frequently asked questions
What is a shell script and why should I use it?
Shell script is a set of Unix/Linux commands written in a text file. It allows you to automate repetitive tasks, combine multiple commands into one, and increase system administration efficiency.
What tools do I need to write shell scripts?
You only need a text editor (like vi, nano, gedit) to write the script and a terminal to execute it. The Linux/Unix operating system has built-in necessary tools.
How to run a shell script?
After saving the script with the .sh extension, you need to grant execution permission with the command chmod +x your_script.sh and then run it using command ./your_script.sh.










