A Shell script is a nothing but a sequence of commands written to achieve a specific task. For example:
echo "script to create a file"
echo "File created"
What is the need of Shell Script?
A script helps to automate tasks. For instance, if you are required to execute 20 commands every day, you will find it difficult to write all the 20 commands one after the other. A better method would be to write those 20 commands in the form of a script and then run the script once.
Writing your First Shell Script
There are three simple steps to create and run a script
Step1: Create a file and write the required set of instructions in it.
So, let’s assume that we want to run three commands (ls, cal, date) one after the other. Create a file (first_script) using any editor and write the three commands in it.
Note: It is optional to give extension to the filename i.e., you may keep the file name as first_script or first_script.sh
Step2: Set execute permission on the file
$chmod u+x first_script
Step3: Run the script as
Once you execute the script as shown above you will see the output of ls, cal and date commands
The Sha-bang(#!) Line
The Sha-bang or She-bang or hash-bang or pound-bang is the first line of a script which tells the system which program to use to interpret the commands in the script. Each scripting language has a different she-bang line. For example:
#!/bin/sh #shell script
#!/bin/bash #shell script
#!/usr/bin/env python #python
#!/bin/bash is the default shell interpreter on a Linux machine.
Is She-bang mandatory to use in shell script?
For shell script it is not mandatory because even if you not write it the system assumes that the script that you are trying to execute is a shell script and hence will use the /bin/bash interpret the script