How To Create Shell Scripts


Table of Contents

Having to type the same command over and over again can be a daunting task and tiresome for that matter. The shell scripts are really easy to create and run saving you from a lot of misery and anguish if you really prefer using the terminal over using the GUI for running tasks.   We are going to use the inbuilt nano editor to create a shell script. The shell scripts usually have a “.sh” extension. Type “nano” in the terminal to open the nano editor.

open nano in terminal

The command will open nano as follows:

linux cli based nano editor

A shell script always starts with – #!/bin/bash Then type your commands after that. I am going to create a script that starts LAMP.

cd /opt/lampp
sudo setsid ./lampp start
echo 'Xampp has been started'
create shell script in nano linux terminal

When done writing commands, press CTRL + X to exit and save the script.

save script in nano text editor

Type “Y” to save the file. Then input your name of the script.

save file in nano rename

I will save the file as “”.

save file in nano text editor

Once done press the enter key to save.​We have successfully created a simple shell script. To run the script you will have to change the permissions to 775. The permissions part is composed of binary numbers that show the permission. The binary numbers include: 4  2  1

The permission levels include:

  1. Read – read permissions have a value of 4
  2. Write – write permissions have a value of 2
  3. Execute- execute permissions have a value of 1.
  4. No permission is granted with a value of 0

To know more about Linux permissions, we’ve dedicated an article to it. Read here.

To give read permission, give values of “4”, to give both read and write permissions assign value “6”, this is gotten from adding the 4 and 2 giving combined permissions of 6. To give read, write and execute permissions, assign the value of “7”, this is given by combining the permissions 4, 2 and 1 to obtain 7.

To view the permissions of all files, type: ls -l To view the permission of a specific file, type: ls -l

check file permissions in linux terminal

The permissions are written in the form of “r” for read, “w” for write and “x” for execute. The permissions groups are three. One for the current user, the second for user groups and the third for others. The above has permission “rw” for the current user, “r” for the user groups and “r” for others. In binary, it’s represented as 644. Every binary is for the different groups. We will need to change our script to “774” to enable it to execute for the current user and the user groups. To change that we will need to change the permissions to read “-rwx-rwx-r”. The “-” shows it is a file.​

To change the permission, you will require typing the following command in the terminal. sudo chmod 774

chmod change file permission in linux

After which input your password if you hadn’t used sudo before and the permissions should change to the following:​If you don’t change the permission, when you try running the script, it will not run and will output the following:

run shell script in linux terminal

To run the script, just type “./” followed by the name of the script to run. In this case: ./

run script in linux terminal

It will now run successfully. Sometimes creating aliases is much easier to automate the scripts. To create an alias, an example is xampp that will run, first you will have to make sure you have a file known as .bash_aliases in your home folder. In case you don’t have a .bash_aliases file, create it.

Type the following to create the file: touch .bash_aliases Once the file is created, open it using nano or any preferred text editor and type – nano .bash_aliases Then type: alias xampp=’.’  

Note: To create an alias type “alias” followed by the alias name such as “xampp” then the command to run. In this case “.”.   In order to avoid using the “/” in “./”, you will have to move the file to “/usr/local/bin”. This will allow you to initiate a command right from the bin folder. When done, save the file and now try the alias as follows:

create alias in linux

It should run as follows without any problem.

run alias in linux terminal

In the end, commands can be much easier to run if you type lots of recursive commands into the terminal. You will find running of tasks much easier when you create scripts that run after typing a simple alias.

I hope you have fun automating your commands by creating a one time script that is saved in your “/usr/local/bin” folder and has an alias attributed to the “.bash_aliases” file. Good luck shell scripting!

Linux TutorialsUncategorizedhow-to