How to Create Shell Script in Linux/Unix

Today, We will learn how to create shell scripts in Linux or UNIX systems. We will start with the basic concept of how shell scripts work and will also see the example. Read: Linux Shell script example 1

Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

Why do we need to write a shell script?

  • To avoid repetitive work and automation
  • System admins use shell scripting for routine backups
  • System monitoring
  • Adding new functionality to the shell etc.

What is Shell? Read: The Linux Shell

Shell is a UNIX term for an interface between a user and an operating system service. Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program’s output in a shell script.

understanding of shell script

There are many shells available for Linux, Few popular shells are here. Each shell does the same job but understands the different commands.

  • BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
  • CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
  • KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications etc.

Example of Shell script : Read also Shell Script Example – Linux


# Author : Zara Ali
# Copyright (c)
# Script follows here:

echo "What is your name?"
echo "Hello, $PERSON"

Here is a sample run of the script −

What is your name?
Hello, Satya

Usually, shell scripts are interactive means they accept input and process with some command and display result. However, sometimes we used to write shell scripts to automate the tasks or do a bunch of jobs.

A shell script has syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++, etc. it would be very easy to get started with it.

  • Shell Keywords – if, else, break etc.
  • Shell commands – cd, ls, echo, pwd, touch etc.
  • Functions
  • Control flow – if..then..else, case and shell loops etc.

What are Shell Variables?

As we know, Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information and they can be by the shell only.

For example, the following creates a shell variable and then prints it:

variable ="Hello"
echo $variable

Below is a small script that will use a variable.

echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"

Read more: Shell Script Example – Linux, for Loops in Shell Script

Leave a Reply