Introduction to Shell Scripting

A Shell provides you with an interface to the UNIX . It gathers input from you and executes programs supported that input. When a program finishes executing, it displays that program’s output.

Usually Shells scripting is an interaction with system, it accept command from users as input and execute them. However a while we would like to execute a bunch of commands routinely, so we’ve type altogether commands whenever in terminal.

As shell also can take commands as input from file we will write these commands during a file and may execute them in shell to avoid this repetitive work. Files with runs these scripts are generally known as shell programs or scipts. Shell scripts are almost like writing commands in word file. Each shell script is saved with .sh file extension eg. simplescript.sh

Why do we need shell scripts?

There are many reasons to write down shell scripts:

1] To avoid repetitive work and automation

2] Administrator do routine backups using shell scripting

3] System monitoring

4] Adding new functionality to the shell etc.

Types of Shell

There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:

POSIX shell also is known as sh

Korn Shell also knew as sh

Bourne Again SHell also knew as bash (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:

C shell also is known as csh

Tops C shell also is known as tcsh

How to Write Shell Script in Linux

Let us understand how to write a Shell Script:

  1. Create a file using a nano editor(or any other editor). Name script file with extension .sh
Fig: opening file using text editor.

2. Start the script with #! /bin/sh

Fig: Shell script using nano editor.

3. Write some code. You can add comment using # in the starting of the line.

Fig: ls code to list files.

4. Press Ctrl + o to write the file and then Save the script file as filename.sh

5. Execute the script by typing bash filename.sh

Fig: executed file output.

Advantages of shell scripts:

  1. The command and syntax are precisely the same as those directly entered in instruction , so programmer don’t got to switch to thoroughly different syntax.
  2. Writing shell scripts are much quicker.
  3. Quick start.

Disadvantages of shell scripts:

  1. Slow execution speed.
  2. Some Design flaws are in the language implementation or syntax.
  3. Provide minimal arrangement unlike other scripting languages. etc

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store