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.
We can also use sh to run the script as it will direct to the default shell in the setup environment.
sh filename.sh
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