How to use bash or sh to Run Bash Script in Linux In Linux

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.

bash filename.sh

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. 

Using the bash command to run the script.

We can also use sh to run the script as it will direct to the default shell in the setup environment. 

sh filename.sh

Using the sh command to run the bash script.

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.

How To Run Bash Script In Linux?

Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash script in Linux? In this article, we will explore various methods for executing Bash scripts and provide valuable insights on best practices.

Table of Content

  • The Shebang Line:
  • Using bash or sh to Run Bash Script in Linux
  • Using Source to Run Bash Script in Linux
  • Making the Script Executable with chmod to Run Bash Script in Linux
  • How To Run Bash Script In Linux – FAQs

Similar Reads

The Shebang Line:

Before we delve into the methods of running Bash scripts, it’s important to understand the shebang line. The shebang line, which is the first line in your script, indicates the interpreter that should be used to execute the script. For Bash scripts, this line should typically look like this:...

Using bash or sh to Run Bash Script in Linux

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....

Using Source to Run Bash Script in Linux

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....

Making the Script Executable with chmod to Run Bash Script in Linux

This method allows you to run a Bash script as an executable, which means you can run it from anywhere in your environment as long as you have a Bash shell available. To make the script executable, you need to adjust its file permissions using the chmod command....

How To Run Bash Script In Linux – FAQs

How do I run a Bash script in Linux?...

Conclusion:

In this article, we’ve discussed various methods for running Bash scripts in Linux and provided best practices to ensure a smooth execution. By including a shebang line in your scripts and choosing the appropriate method for your needs, you can harness the full power of Bash scripts to automate tasks and streamline your command-line workflow. Whether you prefer using bash, sh, or source, or making your scripts executable with chmod, these methods empower you to run Bash scripts effectively and efficiently in a Linux environment....