The use of Linux operating systems revolves around the efficient use of command lines in the terminal. In many cases, the terminal commands may need to be written inside a file so that these commands can be saved and run in sequences. These files are called script files and are saved with the extension “.sh”. These files can be run in various ways so that they are able to perform the multiple commands present inside the files in a standard sequence.
This article will elaborate on how the “.sh” file can be executed inside a Linux Operating System.
How to Run a “.sh” File in Linux?
There are several ways using which, any file with the “.sh” extension can be executed in the system. Let’s take a look at the different methods to run a “sh” file:
Method 1: Using the “sh” or “bash” Command
The first and most simple method to execute a “.sh” file is to use either the “sh” or “bash” command in the Linux terminal alongside the name of the file. This will simply execute the commands on the file. The following command runs the “sh” file using the “sh” keyword:
$ sh fileName.sh
The same file is executed using the “bash” keyword as follows:
$ bash fileName.sh
The examples above demonstrate the working of these two commands for the file name as “sample.sh”. Although these commands are very simple to execute, they can not execute files that have a more complex bash script. The more complex bash files will require the use of many different permissions from local or root users.
Method 2: Make the File Executable
Another extremely common practice is to make the “.sh” file executable using the terminal and then run it. To make a file executable, simply run the command below:
$ chmod u+x fileName.sh
To check if the file has become executable or not, run this command:
$ ls -l fileName.sh
The sample output for these commands is displayed below where “sample” is the file name:
$ chmod u+x fileName.sh $ ls -l fileName.sh
The x in the output proves that the file is indeed executable. Now that the file is executable, you can simply run the file using the command below:
Method 3: Make File Executable Using GUI
Apart from the command line support, a file can be made executable from the GUI. The first step is to open the directory where the script file is placed:
Right-click on the file and select “Properties” as demonstrated in the snippet below:
Once a dialog box opens up, click the “Permissions” tab and check the “Execute” box to make the file executable as shown below:
This is the GUI method of making the file executable after which the file can be run using the following command:
That’s it! You have learned several ways to run a “sh” file.
A “sh” file can be executed simply by running the “bash” or “sh” command. If you want to execute the file as a program then you need to make the file executable which can be done using the terminal or the GUI on the system. This article has explained and demonstrated all of these methods to run as well as make the “sh” file executable.