shell script

Shell Script – Start scripting now

Shell Scripts – What are these?

We usually types command in the Shell of the Linux for our small and naive tasks, but what if we want to run a lot of commands or maybe we want to run a bunch of commands regularly? So here comes the solution. We can create a file with all these commands which you want to be executed in some manner. And the file which has all these commands is called as Shell Script.

So now you must be thinking “that’s easy”. Yes it is, but there are few rules of scripting, just like any other programming language, and if you remember those while writing shell script, you will have an excellent control over your set of commands, and an automated working script.

Benefits of Shell Scripts:

  1. You can have automate your task minimizing the chance of mistake (that might happen if you do it manually).
  2. You can create your own commands with shell script, which might be a set of small commands.
  3. It can take input from the user or from a user or from file or may be from some other resource also provides output to screen.
  4. Saves lots of time.

How to write Shell Script:

Generally we use vi editor to create the shell scripts. There are also other text editors available which can be used for writing scripts in Linux.

Let’s type our first script in vi editor and name it as

#! /bin/bash
# Talent Cookie's First Script
echo "Welcome to Talent Cookie !"

In order to comment out lines we use character (#). All the characters on the right side of # are ignored by the shell. However it’s different for the first line of the script which is the interpreter line and begins with #! (Called shebang). This helps the system in knowing that the command is executed by the Bourne Again shell (BASH) (and yes there are multiple shells too, but that’s out of scope for now).

We have our first script ready now, but we have to run it and for that we have to provide permissions to it. And if you are confused about permissions, than you can read our post Unix And Linux File Permissions , and then continue with below stuff.

Giving permissions to Shell script:

In order to execute the script we should make it executable. As you must be aware of chmod command to change the permissions of a file. Hence to make the file executable we use below command:

chmod 755

Above will give read, write and execute permission to you. Its gives only read and execute to permission to other users and groups. If you want only you can read, write and execute than give the permissions as 700. We won’t go into details or permissions as we assume you might have read above recommended post.

Now to execute your script use below command.


You will get the output as:

Welcome to Talent Cookie !

Things seems pretty easy till now. Let us go into some more details and try to make it more interactive and better than earlier (Oh yeah! You are going to love this). If we want to make our scripts interactive i.e. if we want input from the users we can do it using read statement in our scripts.

Use can very easily understand this by below example. Let’s name the script as

#! /bin/bash
# Details of the new employee
echo "Enter the name of the employee "
echo " Enter the department of the employee "
echo " Welcome $EMPNAME of $DPTNAME department "

Now if we execute the script it will ask you to first enter the name of the Employee and then the concerned department.

$ ./
Enter the name of the employee
Enter the department of the employee
Welcome Khushi of Finance department

So far we learned how to take input, make shell script executable and how to run it. But that was the starting, and we have the core basics still waiting for you. 🙂

 Variables in Shell Scripts:

Similar to any programming language shell scripts too have variables, but they don’t have explicitly defined data types for them. The variable here are needed to be defined in a simple manner with just variable name and its value, and yes it can hold any type of the value provided to it. Variable in shell scripts is defined as:


For example, if we want to save a person’s details as follows:

Name – Ram

Age – 31

We can add it to shell script’s variable by just defining the variable name and assigning its value.

#! /bin/bash
echo $NAME
echo $Age

Use $ sign to access the values of the variable in the scripts. And the results gets printed as follows:


So we understood how we can make use of variables  to save different type of data into it and print it to the screen.

Till now we have learned what is a shell script, what are its benefits, how to quickly write a simple shell script, giving permission to it, how to print anything on the screen, how to take input from the screen and how to save values in variables and print them.

That’s all for this post, but we have a bunch of post following this one, so you just have to be keep on visiting to take bite of knowledge. 🙂

And don’t forget to comment below, if you like it, even if you don’t like it.

Related Posts

One thought on “Shell Script – Start scripting now

Leave a Reply

Your email address will not be published. Required fields are marked *