Open In App

How To Run Bash Script In Linux?

Last Updated : 10 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Bash scripts, also called shell scripts, are programs that help automate tasks by storing a series of commands that often go together, like updates or upgrades. These scripts make it easier to perform tasks automatically instead of typing each command manually. After creating a Bash script, you can run it in different ways using the command line or terminal.

In this article, we will explore the different methods to run Bash scripts in Linux and share some useful tips for doing it effectively.

The Shebang Line

Before running a Bash script, it's important to understand the shebang line, also called the hashbang or pound-bang 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:

#!/bin/bash

This line informs the system that the Bash shell should execute the script. Even though some methods may work without this line but including the shebang line is considered good practice to ensure your script works correctly across different systems.

Structure of the Shebang Line

The shebang line follows this format:

  • #! : These two characters indicate the start of the shebang line.
  • /path/to/interpreter : This is the full path to the interpreter that will execute the script, such as /bin/bash or /usr/bin/env python3.
  • [optional-arguments] : You can also add arguments to be passed to the interpreter if needed.

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.

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.

Using Source to Run Bash Script in Linux

The source command is a simple way to run a Bash script. To execute a script using this method, we just need to type in "source" before the file/script name with an extension in the terminal. For example:

source filename.sh

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.

Using Source to run a bash script

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 very basic, 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.

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.

First, navigate to the directory where your script is located. Then, run the following command to change the file's mode, making it executable:

chmod +x filename.sh  

The `+x` flag indicates that the file should be executable. If you are using Linux and are not the root user, you may need to use `sudo` before the `chmod` the command for permission.

After you've granted execution permission, you can run the script with the following command, assuming you are in the same directory as the script:

./filename.sh

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.

./pathToTheFile.sh
using chmod and executing the script.
Executing a script from a relative path.

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.


Next Article
Article Tags :

Similar Reads