In this introduction chapter, we shall look at below topics
1.1 Now what is a shell script?
1.2 what are the uses of shell script?
1.3 How to create a shell script?
1.4 Writing your first shell script
1.5 How to execute a script?
1.6 Shell Comments
1.7 The “echo” command
1.8 Colouring the output
A shell is an interface between kernel and user. Shell accepts commands from the user and executes it in the kernel and shows the output to user. Initially there was no GUI for Linux, users used to interact with the shell with the help of terminal. In this tutorial we shall learn about shell scripting, a computer program to execute in the shell.
Bourne shell is the most popular shell today.
To know the type of shells you have, type “cat /etc/shells”. This command will give you the list of shells present in your OS.
For me if I type “cat /etc/shells”, then it will give me the below output.
/bin/sh
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
“sh” stands for Bourne shell. This was the original shell. It is still used in Linux/Unix. “bash” stands for “Bourne Again Shell”. This is an update on “Bourne shell”. Now a days “bash” is used as a standard. In this tutorial we shall learn about “bash”.
To know where the bash is located, you need to type the command “which bash”. In my system it will produce below output:
/bin/bash
1.1 Now what is a shell script?
A shell script is a series of command written in plane text. A shell script will and with “.sh” extension. Although the extension is not mandatory, it is recommended.
1.2 what are the uses of shell script?
1. It is used to automate regular works.
2. It can be used to create a new command using combination of commands.
1.3 How to create a shell script?
A typical shell script file can be created by using basic editors like vim. The file should have a “.sh” extension. Then you need to give the execute permission for that script to run.
1.4 Writing your first shell script:
#!/bin/bash
#this is a comment
echo "Hello World"
The Shebang “#”
So every shell script we should specify which interpreter should be used. This should be specified in the first line by using “#” following the absolute path to the bash interpreter.
Example:
#!/bin/bash
if you don’t specify the interpreter line, by default it will use “/bin/sh”.
How to execute a script?
Let’s suppose you have have created a “testScript.sh” file. Now there are 2 ways to execute the script.
Method 1:
sh testScript.sh
or
bash testScript.sh
Method 2:
You need to give execute permission to the file by using “chmod”.
chmod +x testScript.sh
Then
./testScript.sh
1.6 Shell Comments:
Comment are the way to inform other developer about the code flow. That other developer might be you, at a later point in time. A good written program should add comments wherever necessary.
In shell script we use hashtag “#” to indicate a comment. If there is any line staring with “#” it will be treated as a comment.
1.7 The “echo” command.
“echo” is a command that is used to display the output to the user.
For example:
echo “hello world from prodevelopertutorial.com”

Syntax for using colour codes:
echo -e “\033[<colour_code>m Your text”
Here “\033[” is the escape sequence for colour output. “\e[” can also be used.
<colour_code> is the code to be entered.
m It terminates the escape sequence.
Example:
echo -e “\033[31m this will be red color”
this will be red color
echo -e “\033[32m this will be green color”
this will be green color
echo -e “\033[43m this will be in yellow background color”
this will be in yellow background color
echo -e “\033[0m this will return to normal mode”
this will return to normal mode
Output of the above commands:
So by using what we learnt in the above steps, let’s execute our first shell script:
Step 1:
Open a text file using vim or nano.
vim helloWorld.sh
Step 2:
Copy paste the below content in the text file.
#!/bin/bash
#this is a comment
echo “hello world”
Step 3: Close the file.
Step 4: Give permission
chmod +x helloWorld.sh
Step 5: Execute the script to get the below output:
Note: Unlike other languages like C, C++, there is no need to enter semicolon at the end of the statement.