Categories: TutorialsUbuntu

How to Run .sh File Commands All in Once in Linux

In Linux distributions, the bash shell environment is the key element for managing the system components. Several commands are executed on the bash shell to perform a specific task. All bash commands are mostly stored in a bash script file with the ‘.sh’ extension. The .sh file is a shell script that is used to install an application or to perform various jobs in Linux / UNIX operating systems. The main benefit of writing the shell script is that you do not need to write different commands again on terminal windows.

This tutorial will help you to create and run the .sh file or bash scripts using the terminal on the Linux system.

Run
Sponsored
.sh file through the Terminal

In this method, you learn how to run sh file using the Terminal application.

Create .sh file in Linux

First, create a shell script file that we will run through the terminal. Open the text editor and add the following content to this file.

echo "Hello this is my first shell script."

Save it with ‘testscript.sh’.

Make file executable

After creating the sh script file, it is important to set some executable permissions. Otherwise, you will receive an error of ‘permission denied’ on your screen.

To make this sh file executable use this command:

$ chmod +x filename.sh

Here, name of the file is ‘testscript.sh’.

$ chmod +x testscript.sh

You can verify whether the required file is executable or not by executing the following command:

$ ls -l testscript.sh

This output will receive on the terminal that represents sh file as executable.

Run the sh file

Using the two different methods, you can run sh script using the terminal on a Linux system.

Sponsored

The first method to run sh script is by calling the name of the file with its complete.

$ ./testscript.sh

The alternative method to run sh file is by executing the bash command.

$ bash ./testscript.sh

The current user with having sudo privileges can also run the sh file as:

$ sudo ./testscript.sh

You can also debug the shell script by using the ‘-x’ option.

$ bash -x ./testscript.sh

Run the sh file using the Graphical interface

If you are using the GUI environment in Linux, you can also run sh file in the following steps:

  1. Select sh file that you want to open on your Linux system.
  2. Right-click on the file icon and choose ‘Properties’ from the drop-down list.

  1. Now, go into the ‘Permissions’ tab and check the box ‘Allow executing file as a program’.

  1. Click on the file. You will select ‘Run in Terminal’ and you will notice it will be executed in the terminal.

Conclusion

We have seen how to run sh files using the terminal as well as using the command line or terminal. We explored various commands that are used for running sh files. Using the shell script, you can easily solve complex problems using a short script that also saves our time.

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

Canonical announces 12 year Kubernetes LTS

Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…

18 hours ago

Ubuntu Weekly Newsletter Issue 878

Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…

2 days ago

How your feedback shapes the way we support open source software

At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…

2 days ago

How To Install osTicket v1.14 On Ubuntu 20.04

I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…

3 days ago

How To Install WordPress On Ubuntu 20.04

Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…

3 days ago

How To Install DNS Server (Bind9) On Ubuntu 20.04

Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…

3 days ago