How To Run Bash Script In Linux?
The true power of a Bash script is utilized when it is run. But how to do that? Well, there are a plethora of ways to run a Bash script( shell script). Some of them may be useful in certain conditions, while it doesn’t matter how you run the script. Bash scripts are usually executed in the terminal or command-line interface.
To run a Bash script there are many ways. Some of them are given below:
- Using bash or sh.
- Using source.
- Running directly in a bash environment.
For making some of these methods work, the script must have a shebang as the header to indicate it’s a shell script or bash script in this case. So, be sure to include the command below at the top of the file.
This command will make the script run under the bash interpreter. It is recommended to write the shebang header even if it works without them.
Using bash or sh
This is the most standard way of executing the bash script. You must have git bash installed if you are using Windows. For Linux and macOS, bash is installed by default. In this method, we type bash followed by the file name with extension i.e. sh in this case. In a terminal, run the following code by replacing the filename with your bash script filename.
Here, bash is a program that contains the shell environments necessary to run the script from the bash shell. So this will execute the script from the bash interpreter.
We can also use sh to run the script as it will direct to the default shell in the setup environment.
From the above example, we were able to run a bash script using bash as well as the sh command. If you are not in the same folder/directory as the script, make sure you specify the relative path to the script.
This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename.
The script will simply get executed after “sourcing” the file. The source command will execute the shell script as the default bash command provided you are in the bash shell. You need to be in the bash shell to execute the script using the source command.
From the screenshot of the script running, we can see that the source works exactly like the bash or sh command. The above script is a very basic script, but that doesn’t matter as long as the script is errorless and bug-free. Also, you need to add the relative path here as well if you are not in the same directory as the bash script.
By specifying the path to the script and chmod
This is a standalone method to run a bash script. We have to execute the script as an executable, we can run the script anywhere provided we have a bash shell somewhere in the environment. To make it executable we need to make sure we have the rights to run the file as an executable. We will use chmod for changing the rights on the file/script. In a terminal, run the following code by replacing the filename with your bash script filename.
chmod +x filename.sh
The above command will allow us to execute the file. So it changes the mode of the file, the file should be read-only, executable, or any other mode for files. If you are using Linux and are not the root user, simply use sudo before the command chmod. The +x command will make sure the file is executable by everyone in the environment.
After the permission of the file is taken care of, we can now simply execute the file as follows. The command below takes into consideration that you are in the same directory as the file/ bash script is in.
If you are not on the same path as the bash script, make sure you provide the relative path to the file or the bash script.
The above snippets and screenshots show that we can run the scripts in a bash environment by changing the mode of the file using the chmod.
From the following guide, we were able to run scripts in Linux using various methods and programs. So, those were some methods to run a bash script on Linux or pretty much anywhere.