• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

How to write Shell Script in Linux/Unix

AnonyViet by AnonyViet
January 30, 2023
in Tips
0

Shell Scripting is an open source computer program designed to be run by Unix/Linux shell. Shell Scripting is a program that writes a sequence 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 of the AnonyViet 👉 Link 👈

In this article, you will learn from the basics of Linux/Unix shell scripting 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, shell types 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 the commands the user enters 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:

shell scripts

Kernel is the kernel of the computer. It enables the system to communicate between hardware and software. While the Kernel is the innermost part of the operating system, the shell is the outermost part.

The Linux shell takes input from the client side as a command, processes it, and then outputs the output. It is the interface through which the user can work on programs, commands and scripts. Shell is accessed by terminal.

When you run the terminal, the shell gives a command prompt (usually $), where you can type 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 fragile interior of the Operating System, protecting it from accidental damage. Hence the name Shell.

Types of shells

There are two main types of shells in Linux:

  1. Bourne Shell: The prompt for this shell is $ and subshells include:
    • POSIX shell also known as sh
    • Korn Shell is also called sh
    • Bourne Again Shell also known as bash (most common)
  2. C shell: The prompt for this shell is % and various types like
    • C shell also known as csh
    • Top C shell 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. 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 where the shell can find.

Steps to create Shell Script:

  1. Create files with vi editor (or any other text editor). Name the script file with .sh . extension
  2. Start the script with #! /bin/sh
  3. Write a few lines of code
  4. Save the script file as filename.sh
  5. Execute the script file with the command bash filename.sh

“#!” is an operator called 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 a Shell Script program in Linux/Unix:

How to write Shell Script in Linux/Unix 5

The ‘ls’ command is executed when we execute the sample.sh script.

Commenting is a very important element in any program. In shell programming, the syntax to add comments is, the content after the # will have no value, the purpose is to explain the author’s code.

#comment

Eg:

How to write Shell Script in Linux/Unix 6

What is a variable in a shell?

As discussed earlier, variables store data in the form of characters and numbers. Similarly, shell variables are used to store information and they only work within the shell.

For example, create a shell variable and then print it out:

variable ="Hello"
echo $variable

Here 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 the script:

How to write Shell Script in Linux/Unix 7

As you can see, the program has selected 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.

The article achieved: 5/5 – (100 votes)

Tags: LinuxUnixscriptShellwrite
Previous Post

Instructions to make iPhone freeze with Text Bomb containing Sindhi language

Next Post

Disable other people’s Internet with WifiKill For PC (NetCut )

AnonyViet

AnonyViet

Related Posts

Get free Cambridge courses to prepare for IELTS, Starters, Movers
Tips

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to record reaction videos with Android phones, no app needed
Tips

How to record reaction videos with Android phones, no app needed

June 1, 2026
Instructions on how to get Google AI Pro 1 year for free for new accounts
Tips

Instructions on how to get Google AI Pro 1 year for free for new accounts

June 1, 2026
Top free AI tools to help write, test and optimize content
Tips

Top free AI tools to help write, test and optimize content

June 1, 2026
Instructions for receiving Lovable Pro 12 months for free
Tips

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
Instructions for receiving 1 month of ElevenLabs Creator for free worth
Tips

Instructions for receiving 1 month of ElevenLabs Creator for free worth $22

May 25, 2026
Next Post
Disable other people’s Internet with WifiKill For PC (NetCut )

Disable other people's Internet with WifiKill For PC (NetCut )

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to check network speed on Apple TV when the image is jerky or lagging

How to check network speed on Apple TV when the image is jerky or lagging

June 3, 2026
Instructions on how to prevent acquaintances from seeing your Tiktok

Instructions on how to prevent acquaintances from seeing your Tiktok

June 2, 2026
How to record reaction videos with Android phones, no app needed

How to record reaction videos with Android phones, no app needed

June 1, 2026
Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to check network speed on Apple TV when the image is jerky or lagging

How to check network speed on Apple TV when the image is jerky or lagging

June 3, 2026
Instructions on how to prevent acquaintances from seeing your Tiktok

Instructions on how to prevent acquaintances from seeing your Tiktok

June 2, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Get free Cambridge courses to prepare for IELTS, Starters, Movers

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to check network speed on Apple TV when the image is jerky or lagging

How to check network speed on Apple TV when the image is jerky or lagging

June 3, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply