What is a Shell Script?

A shell script is a Linux-based script in which commands are written, and when a user executes the script, all those commands that are in the script are executed one after another. Think of it this way: You have a task you need to do that requires a certain number of commands to be written by the user, so it’s difficult to write and then execute those commands one at a time, that’s where the shell script comes in.

To accomplish this task, simply write all of these commands into a single script and save that script file somewhere. Then, when the user needs to do a particular task, all they have to do is run the saved script and the task is done without having to write all the commands again one by one. The shell is an interpreter of the commands that the user writes.

How to write a Shell Script?

We will use one of the built-in editors in Ubuntu 20.04 LTS to write a simple script. The editor is called ‘Nano’ and the shell scripts have a “.sh” extension. File extensions on Linux are optional, but it is a good practice to name shell scripts with the “.sh” extension.

Now, at the very beginning, you need to type “nano” in the terminal window to open the text editor. This is usually already installed in Ubuntu.

Open the Nano Eduitor

The above command will open the Nano editor that will look something like this:

Nano Editor Window

The script usually starts with #!/bin/bash so you first need to write this. So, the list of commands goes as follows:

echo "welcome"
echo "this is the whole list of dir"

When you are done with the commands that are mentioned above then you press CTRL + X to save the script and exit. After that, the system will ask you for confirmation and then it will ask you to write a name for the file. I will be saving the file as “directories.sh”. It will look like this:

Save modified buffer

Press “y” to confirm.

File name

After you do this the editor will exit and save your script.

Till here you will have successfully created a simple script, now the script has default permission of rw – -r- -r (the first flag is for the current user, the second one is for user groups and the third one is for others). The permission format consists of binary numbers that represent permissions. The basic permissions are 4 2 1.

  • Read- read permission is assigned to 4
  • Write- write permission is assigned to 2
  • Execute- execute permission is assigned to 1

So, to execute the script you saved you need to change its permission to 7 7 4. the concept of this is that if you want to give an rwx (read, write, execute) you will have to add 4(read)+2(write)+1(execute) that will sum up to 7. so the permission of that script needs to be 774 for the user to execute it.

Make the Shell script executable

You can check the permission of a specific file by the following command:

 ls - l directories.sh

Now, to change the permission of the script you saved, you will have to write the following command in the terminal:

sudo chmod 774 directories.sh

Give the script executable permissions

After you press enter it will ask you for the current user’s password, after entering the password it will change the permissions for the file.

Now, to run the script you just have to type “./nameofscript”.


Run the script

You will get something like this as a result of executing the script. Now, if you followed all the steps carefully and as mentioned above then you will have created a script and executed it successfully.

Easy Bash script access using Aliases

To make it easier to automate the script, you create an alias for the script you made.

First of all, you need to make a file called .bash_aliases in your home folder. You will have to enter the following command in the terminal:

touch .bash_aliases

Create a bash aliases script

Once it is created, open the file by using the command:

nano .bash_aliases

Edit bash aliases file

After you enter the above command Nano will open and look like this.

Then you will have to type:

alias dir= ".directories.sh"

In this way, an alias for the script you made will be saved and this alias saves the user the trouble of writing “./script.sh” every time you need to execute the script.

So after you create the alias, all you have to do is type “dir” in the terminal and the script will run.

So, that’s the end of the article, we have covered almost everything about shell scripts. If you follow all the steps carefully and as described in the instructions, you will have successfully created your first simple script. Now you know what a shell script is, you know how to create a script, you know about the permissions system of files and you know how to create aliases. So good luck creating more scripts that will help you.

How to Write a Shell Script in Ubuntu 20.04 LTS

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn