Run .sh File Commands All in Once in Linux
- 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.
Here at LinuxAPT, we shall look into how to create and run the .sh file or bash scripts using the terminal on the Linux system.
Here, you will learn how to run sh file using the Terminal application.
1. To create .sh file in Linux:
Start by creating 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."
Then, Save it with 'testscript.sh'.
2. 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
The output you will see on the terminal that represents sh file as executable.
3. Run the sh file
Using the two different methods, you can run sh script using the terminal on a Linux system.
The first method to run sh script is by calling the name of the file with its complete:
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
If you are using the GUI environment in Linux, you can also run sh file in the following steps:
This article covers how to run sh files using the terminal as well as using the command line or terminal. In fact, the .sh file is nothing but the shell script to install given application or to perform other tasks under Linux like operating systems.
How do I run .sh file shell script in Linux?
The procedure to run the .sh file shell script on Linux is as follows: