Linux Commands

How To Run the .sh File Shell Script In Linux / Ubuntu?

How To Run the .sh File Shell Script In Linux UNIX

The “.sh” Shell Script Files are scripting files that contain scripting language commands ready to be executed and run by the Ubuntu Shell. These are text files that contain programming language code that may be used to carry out automated tasks or create programs or applications.

This article explains the process to run the “.sh” Shell Script Files in Ubuntu.

How To Run the .sh File Shell Script In Linux / Ubuntu?

The “.sh” Script File in Ubuntu can be executed using the Command Line, i.e. Terminal. The “.sh” Script Files can run in the Terminal using either the “sh” command or the shortcut method using the “./” line with the script file. Both the methods are explained below.

Run the .sh File Script in Ubuntu using the “sh” Command

To run the Script file in Ubuntu, ensure it is executable before using the “sh” command. To confirm the executable file, use the command:

ls -l <fileName.sh>

This will return the permissions of the file. In our case, we have a “scriptFile.sh” Script File:

To check the permission, the command used will be:

ls -l scriptFile.sh

The “-rwxrwx” confirms the Script File has the Read, Write and Execute Permissions:

Now to execute and run the Script File, the command “sh” will be used:

sudo sh <fileName>.sh

In our case, the file name is “scriptFile.sh”, so the command to run our Script File will be:

sudo sh scriptFile.sh

This will run the script file and the output of executed script can be seen in the terminal:

The “Hello World!” output in the terminal confirms the Script was executed using the “sh” command in Ubuntu.

The “-v” flag can also be used with the “sh” command:

sudo sh -v scriptFile.sh

It will return the script inside the file as well as the output:

Run the .sh File Script in Ubuntu using the “./” Shortcut

The shortcut method directly runs the “.sh” Script file in Ubuntu using the “./” following the file name. The syntax to use the shortcut method is:

./<fileName>.sh

Before executing the Command, ensure the directory contains the Script File. In our case, to execute and run the “scriptFile.sh” Script File in Ubuntu, the command used will be:

./scriptFile.sh

The script file will be executed directly in the terminal of Ubuntu:

The script file runs and the output “Hello World!” is displayed in the terminal. This is how you can directly run script files from the terminal.

Conclusion

The “sh” command and the “./” shortcut method both are useful ways to execute the “.sh” File Shell Script in Ubuntu. The “sh” command can also be used to display the input lines of the script file using the “-v” flag whereas the “./” command directly runs the script without any command when working in the same directory. This article explained both methods to run the “.sh” Shell Script Files in Ubuntu.

Similar Posts