×


Different methods to create and run a Shell script on Ubuntu 20.04 LTS ?

Shell scripting is regarded as an alternate method of running the commands via the terminal. This is a better 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.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related queries.
In this context, we shall look into the different methods of creating and running a shell script in Linux (Ubuntu 20.04).

Methods to create 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:

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:
a. 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.
b. 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 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.

c. 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.
d. 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

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:
a. You need to click on the File Manager icon located on the taskbar.
b. 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.

c. 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.

How to execute 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:

i. 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.

ii. 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.

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.

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.

[Need urgent support in fixing Linux related errors? We are available to help you. ]


Conclusion

This article will guide you on different methods of creating and running a #shell #script in #Ubuntu 20.04. Shell Scripting is an open-source #computer program designed to be run by the #Unix / #Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. To write and execute a script: 1. Open the #terminal. Go to the directory where you want to create your script. 2. Create a file with . sh extension. 3. Write the script in the file using an editor. 4. Make the script executable with command chmod +x . 5. Run the script using ./.