Ubuntu

How to create and run a Shell script in Ubuntu 20.04 LTS

Introduction:

Shell scripting is considered as an alternate method of running the commands via the terminal. This can prove to be a good alternative of executing the commands through the terminal because a shell script once written can be executed multiple times without having to recall complex commands every now and then. In this article, we aim to talk about the different methods of creating and running a shell script in Linux.

Note: All the creation and execution methods below have been demonstrated on Ubuntu 20.04.

Methods of Creating a Shell Script in Ubuntu 20.04:

There are two basic methods of creating a shell script in Ubuntu 20.04 i.e. the terminal-based method and the GUI-based method and we will be discussing both of these methods one by one:

Method # 1: The Terminal Method:

For creating a shell script in Ubuntu 20.04 by making use of the terminal method, you will have to proceed as described below:

Since this method is terminal-based, therefore, you have to launch the terminal in Ubuntu 20.04 by looking for it in the Activities tab or you can even press Ctrl+ T. The terminal of Ubuntu 20.04 is shown in the following image:

Once you have launched the terminal, you need to execute the following command in it:

sudo nano ShellScript.sh

You can also use any other text editor instead of using the nano editor, however, we have chosen it solely because it is the easiest one to use. Moreover, you can also give any name of your choice to your shell script followed by a .sh extension.

When you will execute this command, an empty shell script file will open up in the nano editor where you can add any piece of code to it. We have simply written “#!/bin/sh” at the beginning of this file to indicate that it is a shell script. Always remember to add this line at the beginning of every shell script. Then we have printed a random message by making use of the “echo” command. After making these changes to your shell script, just save your file and close it by pressing Ctrl+ X.

The script file will be saved in the home directory, by default. If you want to check, execute the following command on the terminal.

ls

Method # 2: The GUI Method:

Another method of creating a shell script in Ubuntu 20.04 is by making use of the GUI which can be done as follows:

You need to click on the File Manager icon located on the taskbar which is also highlighted in the image shown below for your convenience:

Once you are in your Home directory, right-click anywhere to launch a menu and choose the New Document option from that menu. Then choose the Empty Document option to create a file. Give any name of your choice to that file and add a .sh extension after that name. We have named our file as ShellScript.sh. After creating a shell script file, you need to double click on it to open this file and then type the script shown in the following image in your shell file. This script is the same as we discussed in the method above. After typing this script, just save your file and close it.

Methods of Running a Shell Script in Linux:

Now when a shell script has been created, the next step is to run this script which can be done with the help of any three of the methods listed below. All of the methods assume the script file is located in the home directory which is the default path.

Method # 1: Using the “chmod” Command:

For following this method, there are two basic steps that are described below:

Step # 1: Setting the Execute Permission for the Shell Script:

For setting the execute permission for the shell script that you have just created, you need to run the following command in your terminal:

sudo chmod +x ShellScript.sh

Do not forget to add the “sudo” keyword before this command as this will cause your terminal to render an error message.

Step # 2: Running the Shell Script:

Once the execute permission has been set for your shell script, you can easily execute it by running the following command in your terminal:

./ShellScript.sh

After executing this command, you should essentially see the output of the shell script that you have created on the terminal as shown in the image below:

Method # 2: Using the “bash” Command:

The “bash” command can also be used to run a shell script in the following manner:

The significance of using this command to run a shell script is that you do not have to set any permissions before it rather you can execute the script right away by running the following command in your terminal:

bash ShellScript.sh

After executing this command, you should essentially see the output of the shell script that you have created on the terminal as shown in the image below:

Method # 3: Using the “sh” Command:

This is yet another method of running a shell script which can be done as follows:

Again, just like the “bash” command, the “sh” command is also used to save yourself from setting any execute permissions prior to running a shell script. You are only supposed to run the following command in your terminal:

sh ShellScript.sh

After executing this command, you should essentially see the output of the shell script that you have created on the terminal as shown in the image below:

Conclusion:

In this article, we shared with you multiple methods of creating and running a shell script in Ubuntu 20.04. You are free to choose any method of your choice and hence make your life easier with shell scripting.

Similar Posts