How To Write And Execute A Shell Script In Linux/Unix
 

How To Write And Execute A Shell Script In Linux/Unix

 

This post was last updated on August 3rd, 2020 at 03:07 pm

In Unix or Unix based operating systems, a shell is a program that interprets the command and acts as an intermediary between user and kernel. There are different types of shells are available on different Unix based systems. These all have similar functionality but different syntax and capabilities. You can automate a task by writing a script. Other operations of a script are file manipulation, program execution, etc.


What is a shell script

A shell script is a computer program or simply a text file which contains a sequence of command that is being executed by an interpreter called a shell on  Unix or a Unix based machine. There are various dialects are used for different types of shells to write a script which is known as a scripting language. Now we will proceed further in our discussion about the writing and executing a shell script follow the steps that are given below.


Know your shell type

Majorly a shell can be divided into two types –

  • bourne shell (sh)
  • c shell (csh)

There are many other types of shells are also available like bourne again shell (bash), Korn shell (ksh), TC shell (tcsh), Z shell (zsh), etc. but all of these are the descendants and improved versions of either of two that are given above. Since different types of shells have different syntax and capabilities. So it is important to know which type of shell you are using in your system. You can check the shell type by typing /etc/shells in your terminal.

or executing the command echo $0 into your terminal.

Above you can see the type of shell that I am using.


Write your script

A script is nothing but a set of commands that is to be executed on a Unix or Unix based machines. You can use any text editor to write your script. But remember to save it with .sh extension.

As you can see the above script first line contains #! which is known as shebang and it is followed by a symbolic link of the shell. And you can start commenting by using the # symbol. echo is used to display some text.  readis a keyword used to take input from the console. t is a variable contains current time. %r is a format used for 12 hour clock time. date is a keyword used to get date and time.


Make the script executable

By default, a user does not have permission to execute it. So we have to provide permission to execute it use chmod command to change the permission. To know more about file permissions you can read File ownership and permissions in Linux/Unix

here we added executable permissions to all that is user, group and other users.


Execute your script

To execute your script type the following command into your terminal./hello.sh

Once the script executed the output of the above script will be displayed in the terminal as given above. I hope it will develop a good understanding of the topic. If still you have any other query regarding it you can write us in comment below.

Previous How To Add/Remove New User In Linux/Unix
Next Process Management In Linux/Unix
 

About author

You might also like

Linux Topics 0 Comments

What Is apt-fast And Why Should You Use It?

This post was last updated on August 2nd, 2020 at 01:35 pmThe apt-fast is a shell script wrapper for apt-get and aptitude that can drastically improve the downloading speed of

Linux Topics 0 Comments

How To Change User’s Password Through Terminal In Linux/Unix?

This post was last updated on August 1st, 2020 at 12:14 pmLinux or Unix is the multi-user operating system that means more than one user can use system resources simultaneously.

Linux Topics 0 Comments

How To Check System Hardware Information In Ubuntu 20.04 LTS?

This post was last updated on August 1st, 2020 at 12:20 pmA computer system is consists of different types of hardware such as processor, RAM, hard disk, network cards, etc.

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply