• 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

How to download documents on 123doc for free
Tips

How to download documents on 123doc for free

July 21, 2026
Instructions for Logo Design in just a few clicks with Logaster
Tips

Instructions for Logo Design in just a few clicks with Logaster

July 20, 2026
3 coolest ways to turn off your computer
Tips

3 coolest ways to turn off your computer

July 19, 2026
Instructions for checking Windows copyright using Powershell to avoid penalties
Tips

Instructions for checking Windows copyright using Powershell to avoid penalties

July 19, 2026
Who leaked Windows 11?
Tips

Who leaked Windows 11?

July 18, 2026
Instructions for Getting Netflix 30 Days Free to Watch Movies
Tips

Instructions for Getting Netflix 30 Days Free to Watch Movies

July 18, 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

Recent News

How to download documents on 123doc for free

How to download documents on 123doc for free

July 21, 2026
Instructions for Logo Design in just a few clicks with Logaster

Instructions for Logo Design in just a few clicks with Logaster

July 20, 2026
Clean Tam – Accompanying the green space of every Vietnamese family

Clean Tam – Accompanying the green space of every Vietnamese family

July 20, 2026
3 coolest ways to turn off your computer

3 coolest ways to turn off your computer

July 19, 2026
How to download documents on 123doc for free

How to download documents on 123doc for free

July 21, 2026
Instructions for Logo Design in just a few clicks with Logaster

Instructions for Logo Design in just a few clicks with Logaster

July 20, 2026
Clean Tam – Accompanying the green space of every Vietnamese family

Clean Tam – Accompanying the green space of every Vietnamese family

July 20, 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

How to download documents on 123doc for free

How to download documents on 123doc for free

July 21, 2026
Instructions for Logo Design in just a few clicks with Logaster

Instructions for Logo Design in just a few clicks with Logaster

July 20, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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